Search This Blog

Showing posts with label dictionary. Show all posts
Showing posts with label dictionary. Show all posts

Python: test if a dictionary contains a specific key



def contains(dict, key):
return key in dict

dict={}
dict['a']=1
dict['b']=2
dict['c']=3

print(contains(dict, 'a')) # True
print(contains(dict, 'd')) # False

Python: iterate over dictionary


for name in dict.keys():
print(name)
print(dict[name])

values = [ dict[name] for name in dict.keys() ]