Data structures‎ > ‎Dictionary‎ > ‎

get keys


# get all keys that have a 'True' value
d={}
d['Mike']=True
d['Antje']=True
d['Johnny']=False

[key for key, val in d.items() if val]
['Antje', 'Mike']


# get all keys of same value
# get all people born in 1987
d={}
d['Antje']=1987
d['Mike']=1983
d['Johnny']=1987

[key for key, val in d.items() if val==1987]
['Antje', 'Johnny']


# get all keys
sorted(d.keys())
['Antje', 'Johnny', 'Mike']

# get all values (can include non-unique values, for example: '1987')
sorted([val for key, val in d.items()])
[1983, 1987, 1987]