Code Mastery

Enumerate your iterables!

Whenever you write code similar to the following:

you should consider making your code a bit more Python by using the builtin enumerate  function:

The enumerate  function takes an iterable and an optional parameter start  (defaulting to 0). The return value is a tuple containing the index of the element and the element itself.

And remember, by taking an iterable the enumerate function will also work on builtin types such as dictionaries or your own objects that implement the iterable methods.

More information about the enumerate  function can be found in the Python documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *