Iterating over key and value of defaultdict dictionaries
- by gf
The following works as expected:
d = [(1,2), (3,4)]
for k,v in d:
print "%s - %s" % (str(k), str(v))
But this fails:
d = collections.defaultdict(int)
d[1] = 2
d[3] = 4
for k,v in d:
print "%s - %s" % (str(k), str(v))
With:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is…