In [2]:
from platform import python_version
python_version()
Out[2]:
In [3]:
d = {'foo':10,'bar':30,'baz':20}
In [4]:
for key,val in d.items():
print('{} => {}'.format(key,val))
In [5]:
allowed_values = [1, 2]
d = {'foo':1, 'bar':2, 'baz':3, 'quux':4 }
filtered_items = [(k,v) for (k,v) in d.items() if v in allowed_values]
dict(filtered_items)
# >> {'bar': 2, 'foo': 1}
Out[5]:
In [6]:
sorted(d.items(),key=lambda x:x[1])
Out[6]:
In [7]:
sorted(d.items(),key=lambda x:x[0])
Out[7]:
In [8]:
from collections import OrderedDict
In [9]:
d = {'foo':10, 'bar':30,'baz':20}
sorted_list_of_tuples = sorted(d.items(),key=lambda x:x[1])
od = OrderedDict(sorted_list_of_tuples)
for key,val in od.items():
print('{} => {}'.format(key,val))
In [20]:
from collections import ChainMap
d1 = {'a':1, 'b': 2}
d2 = {'b':100, 'c': 200}
d3 = ChainMap({}, d1, d2)
print(d3["a"])
print(d3["b"])
print(d3["c"])
In [10]:
d = dict()
d['foo'] = 10
d['bar'] = 32
d['baz'] = 32
for key,val in d.items():
print('{} => {}'.format(key,val))
In [11]:
od = OrderedDict()
od['foo'] = 10
od['bar'] = 32
od['baz'] = 5
for key,val in od.items():
print('{} => {}'.format(key,val))
In [ ]:
In [ ]: