enumerate() for dictionary in python

On top of the already provided answers there is a very nice pattern in Python that allows you to enumerate both keys and values of a dictionary. The normal case you enumerate the keys of the dictionary: Which outputs: But if you want to enumerate through both keys and values this is the way: Which outputs:

What does enumerate() mean?

The enumerate() function adds a counter to an iterable. So for each element in cursor, a tuple is produced with (counter, element); the for loop binds that to row_number and row, respectively. Demo: By default, enumerate() starts counting at 0 but if you give it a second integer argument, it’ll start from that number instead: … Read more