Pages

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

Wednesday, December 31, 2014

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



Friday, May 9, 2014

python: dictionary comprehension








dict = { 'a':1, 'b':2 }
dict = { k: str(dict[k]) for k in dict.keys() }
print(dict)



Thursday, April 17, 2014

Python: iterate over dictionary








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