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
- reference: create a double dictionary in python
-
the following WON'T work
>>> x = dict() >>> x[1][1] = “foo”
-
instead, one can use this:
>>> x = dict() >>> x[1,1] = “foo”
- note that, tuple (1,1) is used as key.
find the key to the max and min value
suppose d is the dict.
max(d,key = lambda a: d.get(a))
Hide Comments