Checks if the given key exists in a dictionary.
- Use the
in
operator to check ifd
containskey
.
代码实现
def key_in_dict(d, key):
return (key in d)
使用样例
d = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4}
key_in_dict(d, 'three') # True
Checks if the given key exists in a dictionary.
in
operator to check if d
contains key
.
def key_in_dict(d, key):
return (key in d)
d = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4}
key_in_dict(d, 'three') # True