In this post, I'm going to look at Python's `enumerate()` function using Python version 3.5.
What is the enumerate function?
According to the documentation, this function returns an enumerate object when you pass an iterable which must be a sequence, an iterator or some other object which supports iteration. The _ next _ () method of the iterator returned by the enumerate() function returns a tuple that contains a count (which starts from zero unless specified) and the values obtained from iterating over the iterator.

Let's put this into practice with a basic exercise.

Assignment: Given a list of Premier League results for my favourite team in the previous season, print the score for each game week. For example: `Game week 1 - score: 0-2`.

``````# Arsenal's premier league results - season 2015/16

results = ['0-2', '2-1', '0-0', '1-0', '2-0', '2-2',
'0-2', '5-2', '3-0', '2-1', '3-0', '1-1', '1-2',
'1-1', '3-1', '2-0', '2-1', '0-4', '2-0', '1-0',
'3-3', '0-0', '0-1', '0-0', '2-0', '2-1', '2-3',
'1-2', '2-2', '2-0', '4-0', '3-3', '1-1', '2-0',
'0-0', '1-0', '2-2', '4-0',
]
``````

To make this example simple, assume that the scores are in order from the first game of the season to the last and the first figure of each score line belongs to Arsenal (imagine Arsenal played at Home all season because the other teams have crappy stadiums)

The season's results are stored in a list as shown above. The assignment asked us to print a string that contains each week's score and the game week number.

To achieve that, we'll use the enumerate function to return a tuple that contains the count and values from the results list.

``````for index, value in enumerate(results, start=1):
print("Game week", index, "-", "score: ", value)
``````

Easy, right? We use index and value and pass our iterable as argument, and set the start to 1 otherwise the game week will start at 0 by default.

Let's run this code and check out the result.

``````Game week 1 - score:  0-2
Game week 2 - score:  2-1
Game week 3 - score:  0-0
Game week 4 - score:  1-0
Game week 5 - score:  2-0
Game week 6 - score:  2-2
Game week 7 - score:  0-2
Game week 8 - score:  5-2
Game week 9 - score:  3-0
Game week 10 - score:  2-1
Game week 11 - score:  3-0
Game week 12 - score:  1-1
Game week 13 - score:  1-2
Game week 14 - score:  1-1
Game week 15 - score:  3-1
Game week 16 - score:  2-0
Game week 17 - score:  2-1
Game week 18 - score:  0-4
Game week 19 - score:  2-0
Game week 20 - score:  1-0
Game week 21 - score:  3-3
Game week 22 - score:  0-0
Game week 23 - score:  0-1
Game week 24 - score:  0-0
Game week 25 - score:  2-0
Game week 26 - score:  2-1
Game week 27 - score:  2-3
Game week 28 - score:  1-2
Game week 29 - score:  2-2
Game week 30 - score:  2-0
Game week 31 - score:  4-0
Game week 32 - score:  3-3
Game week 33 - score:  1-1
Game week 34 - score:  2-0
Game week 35 - score:  0-0
Game week 36 - score:  1-0
Game week 37 - score:  2-2
Game week 38 - score:  4-0
``````

This `enumerate()` function is so useful and is considered an idiomatic way to write a `for` loop that returns an iterable's values as well index.