Introduction
Part I and Part II of this tip can be viewed from the respective links.
Using the Code
1. Looping over Dictionary Keys and Values
for k in d:
print k, '-->', d[k]
In the above example, we loop over the keys and then lookup the value. It is not very fast because it will have to rehash every key and do a lookup on it.
Instead, do this to get both keys and values.
for k,v in d.items():
print k, '-->', v
Here, we're using tuple unpacking. So when you need both keys and values, loop over them directly and hence no lookups are required. Also, items() makes a list in the memory, which is not required.
for k,v in d.iteritems():
print k, '-->', v
2. Construct a Dictionary from Two Lists
Use zip().
names = ['steve','bill','jeff']
org = ['apple', 'microsoft', 'amazon']
d = dict(zip(names, org))
Pythonic Way: use izip()
from itertools import izip
d = dict(izip(names, org))
Many would think that izip will make a tuple at each iteration, but what it does is after the dictionary has consumed the tuple, we loop back around to make the next tuple, we re-use the previous one, so it can build the dictionary using the same tuple over and over again.
3. Counting with Dictionaries
colours = ['blue','red','green','red','red', 'green']
d = {}
for colour in colours:
if colour not in d:
d[colour] = 0
d[colour] += 1
Better Way: use get() method
d = {}
for colour in colours:
d[colour] = d.get(colour, 0) + 1
from collections import defaultdict
d = defaultdict(int)
for colour in colours:
d[colour] += 1
Although in the above code, d
will be an object of defaultdict
, which will have to be converted to a dict
, if you intend to use it as one!