Quantcast
Channel: Iterating over key and value of defaultdict dictionaries - Stack Overflow
Browsing latest articles
Browse All 4 View Live

Answer by Nguai al for Iterating over key and value of defaultdict dictionaries

If you want to iterate over individual item on an individual collection:from collections import defaultdictfor k, values in d.items(): for value in values: print(f'{k} - {value}')

View Article



Answer by Vlad Bezden for Iterating over key and value of defaultdict...

if you are using Python 3.6from collections import defaultdictfor k, v in d.items(): print(f'{k} - {v}')

View Article

Answer by SilentGhost for Iterating over key and value of defaultdict...

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}")

View Article

Iterating over key and value of defaultdict dictionaries

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] = 2d[3] = 4for k,v in d: print "%s - %s" % (str(k),...

View Article
Browsing latest articles
Browse All 4 View Live




Latest Images