Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Sequences Sequence Iteration Iterating with Enumerate

Pedro Leitao
Pedro Leitao
1,615 Points

Why does she use "index" with the "Enumerate function"?

Why is it typed

for index, item in enumerate(groceries, 1):
    print(f"{index}. {item}")

instead of

for item in enumerate(groceries, 1):
    print(item)

Indeed the output gets a bit different, I am struggling to understand the purpose of "index" though

I don't understand how "index" is getting related to "groceries" and to "Enumerate function"

Thank you all

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

Well you do enumeration in python when you want to keep track of the amount of iterations you are performing, mostly if you need to use them for some form of formatting. Usually you'd have to use other methods for that (a variable that keep track of numbers, indexes in an array, etc.). Python makes this easier to do by having this built in enumerate method.

So basically, the index is basically the most important aspect of it. Yes, you can use the for..in loop to iterate through an iterable object. But the enumerate method will help you keep track of the iterations taking place, and will help format it for you, by even selecting a starting point (eliminating the 0 index issue for lists/dictionaries for example).

Pedro Leitao
Pedro Leitao
1,615 Points

Thanks for your explanation!

But what I don't get is...

That was her first example of a method to enumerate the items in the list. which I do understand the purpose of "index"

groceries = ['roast beef', 'cucumbers', 'lettuce', 'peanut butter', 'bread', 'dog food']

index = 1

for item in enumerate(groceries, 1):
    print(f"{index}. {item}")
      index +=1

That was her second example of a method to enumerate the items in the list. Which I do understand the purpose of the function "enumerate", but not the purpose of the "index"

groceries = ['roast beef', 'cucumbers', 'lettuce', 'peanut butter', 'bread', 'dog food']

for index, item in enumerate(groceries, 1):
    print(f"{index}. {item}")

I would naturally use the enumerate function like that:

groceries = ['roast beef', 'cucumbers', 'lettuce', 'peanut butter', 'bread', 'dog food']

for item in enumerate(groceries, 1):
    print(item)

Thank You again!

Dustin Honeck
Dustin Honeck
12,504 Points

I know this is an old post but I am hoping to add some extra info. If you use the code below, in each iteration of the loop, the variable item will be a tuple containing an index and a grocery item. The print() method will print each of the tuples out but you will not be able to customize how the tuple displays.

for item in enumerate(groceries, 1):
    print(item)

""" Output will look like this:
(1, 'roast beef')
(2, 'cucumbers')
(3, 'lettuce')
(4, 'peanut butter')
(5, 'bread')
(6, 'dog food')
"""

By adding the index variable you are "unpacking" the tuple, which allows you to assign the tuple values to individual variables. This can be beneficial if you wanted to customize the output and not print the tuple out.

for index, item in enumerate(groceries, 1):
    print(f'{index}. {item}')

""" Output will look like this:
1. roast beef
2. cucumbers
3. lettuce
4. peanut butter
5. bread
6. dog food
"""

The enumerate() function creates an object that generates tuples containing the index and the item. The second for loop below is a loose example of what the enumerate() function does. It creates an iterable of tuples. Both for loops below will generate the same output.

groceries = ['roast beef', 'cucumbers', 'lettuce']

# Using enumerate to automatically generate index-item pairs
for index, item in enumerate(groceries, 1):
    print(f'{index}. {item}')

# Manually creating a list of tuples with index-item pairs
for index, item in [(1, 'roast beef'), (2, 'cucumbers'), (3, 'lettuce')]:
    print(f'{index}. {item}')

I hope that helps!