assuming I have a dictionary like:
>>> dict = {}
>>> dict[123] = "test1"
>>> dict[456] = "test2"
>>> dict[789] = "test3"
and I loop through it like:
>>> for bit in dict.keys():
print bit
how can I figure out which element is being processed? I would like to get an output of
0
1
2
How can I get this?
Please advise!
Thanks!
You could use
enumerate():However, bear in mind that
dictdoes not guarantee any particular ordering of its keys, so the indexes are probably not meaningful. There are other classes, such ascollections.OrderedDictthat do guarantee specific ordering of their keys, so you might need to use those.Also, don’t call your variables
dictas this shadows the builtin.