Updated 8 Mar 2019.
enumerate
is a handy built-in Python function that sometimes gets overlooked.
It’s not life-changing, but it makes things a little cleaner and more concise.
Let’s say you want to loop through a list, but you want to access both the list items and the index value for each item. Here’s one way to do it:
However, you can do the same thing a little more gracefully with enumerate
:
items
is similar to enumerate
, but for dictionaries. If you were doing
this…
…try this instead:
items
doesn’t exist in Python 2.7, but iteritems
does the same thing:
Happy coding!