This article is part of in the series

In Python, the enumerate() function is used to iterate through a list while keeping track of the list items' indices. To see it in action, first you'll need to put together a list:

pets = ('Dogs', 'Cats', 'Turtles', 'Rabbits')

Then you'll need this line of code:

for i, pet in enumerate(pets):
print i, pet

Your output should be as follows:

0 Dogs
1 Cats
2 Turtles
3 Rabbits

As you can see, these results not only print out the contents of the list, but also print them out with their corresponding index orders. You can also use the enumerate() function to create an output of the list indices/values with the indices changed according to your code.

for i, pet in enumerate(pets, 7):
print i, pet

This code changes the function so that the first value in the list is assigned the index number 7 for the purposes of your enumeration. In this case, your results would be as follows:

7 Dogs
8 Cats
9 Turtles
10 Rabbits