Check if key is in the dictionary?

This question already has an answer here:

  • Check if a given key already exists in a dictionary 18 answers

  • 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 .

    链接地址: http://www.djcxy.com/p/28884.html

    上一篇: Python安全字典键访问

    下一篇: 检查密钥是否在字典中?