Python dictionary

Python dictionary can only use immutable elements as keys, therefore list can not be used as keys, but tuples with immutable elements can.

Create dictionary

l = dict(a=1,b=2,c=3)
l = dict(zip(['a','b','c'],[1,2,3]))
dict(zip('abc',[1,2,3]))

Iterating Through a Dictionary

>>> import os
>>> for k, v in os.environ.items():     
...     print "%s=%s" % (k, v)

Double dictionary

find the key to the max and min value

suppose d is the dict.

max(d,key = lambda a: d.get(a))
Homepage
Comments

Hide Comments