Iterating over key and value of defaultdict dictionaries
If you want to iterate over individual item on an individual collection:
from collections import defaultdict
for k, values in d.items():
for value in values:
print(f'{k} - {value}')
if you are using Python 3.6
from collections import defaultdict
for k, v in d.items():
print(f'{k} - {v}')
you need to iterate over dict.iteritems()
:
for k,v in d.iteritems(): # will become d.items() in py3k
print "%s - %s" % (str(k), str(v))
Update: in py3 V3.6+
for k,v in d.items():
print (f"{k} - {v}")