python check if key and value in dictionary code example
Example 1: how to check if a key is present in python dictionary
dict = { "How":1,"you":2,"like":3,"this":4}
key = "this"
if key in dict.keys():
print("present")
print("value =",dict[key])
else:
print("Not present")
Example 2: python test if list of dicts has key
>>> lod = [{1: "a"}, {2: "b"}]
>>> any(1 in d for d in lod)
True
>>> any(3 in d for d in lod)
False