Enumerate
enumerate is a built-in function in Python which takes an iterable as an input and returns an enumerate object. A list e.g. [1, 2, 3]
is one of the most common examples of an iterable. Looping over a list can be achieved quite simply, but requires an extra variable to keep track of the index when needed.
months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
month_idx = 0
for month in months:
month_idx += 1
print(f'{month_idx}: {month})
On each iteration enumerate yields a pair of the loop index and iterable value, so a separate variable does not need to be maintained for the index.
months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
for month_idx, month in enumerate(months):
print(f'{month_idx+1}: {month})
The index value returned by enumerate can also be preset with a start value. In the example below month_idx
represents a counter from 1 to 12. Normally an index counter works like birthday years starting from 0 to 1 to 2 and up to 11 for a list of 12 months.
months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
for month_idx, month in enumerate(months, start=1):
print(f'{month_idx}: {month})
Technically, enumerate wraps an iterable with an iterator. Using next demonstrates how this works in action with a tuple pair returned on each call.
months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
iterator = enumerate(months, start=1)
print(next(iterator)) # (1, 'jan')
print(next(iterator)) # (2, 'feb')
Displaying the contents of an enumerate object illustrates how the function behaves at a low-level. The output is a list of tuples containing the counter index and element value per iteration.
months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
print(list(enumerate(months, start=1)))
# [(1, 'jan'), (2, 'feb'), (3, 'mar'), (4, 'apr'), (5, 'may'), (6, 'jun'),
# (7, 'jul'), (8, 'aug'), (9, 'sep'), (10, 'oct'), (11, 'nov'), (12, 'dec')]
enumerate can also be used to loop over a dictionary of key-value pairs with an index counter. Ultimately, regardless of the iterable, enumerate simplifies looping with a counter index variable.