Iterating over key and value of defaultdict dictionaries
Posted
by gf
on Stack Overflow
See other posts from Stack Overflow
or by gf
Published on 2010-05-04T19:03:08Z
Indexed on
2010/05/04
19:08 UTC
Read the original article
Hit count: 303
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 not iterable
Why? How can i fix it?
© Stack Overflow or respective owner