Check if key is in the dictionary?
This question already has an answer here:
Just use the in
operator (which is accompanied by the not in
operator):
if (1, 'a') in edges:
if (1, 'a') not in edges:
Below is an excerpt from the documentation of Python's dictionary type, where d
is a dictionary:
key in d
Return True
if d
has a key key
, else False
.
key not in d
Equivalent to not key in d
.
上一篇: Python安全字典键访问
下一篇: 检查密钥是否在字典中?