How to check if a key-value pair is present in a dictionary?

后端 未结 9 927
無奈伤痛
無奈伤痛 2021-01-03 23:50

Is there a smart pythonic way to check if there is an item (key,value) in a dict?

a={\'a\':1,\'b\':2,\'c\':3}
b={\'a\':1}
c={\'a\':2}

b in a:
--> True
c          


        
9条回答
  •  -上瘾入骨i
    2021-01-04 00:26

       a.get('a') == 1
    => True
       a.get('a') == 2
    => False
    

    if None is valid item:

    {'x': None}.get('x', object()) is None
    

提交回复
热议问题