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

Jacob Truelove
Jacob Truelove
4,194 Points

Why are index/item flipped when we go from creating the loop to setting parameters?

So we say -- for index, item in enumerate(groceries, 1) -- we list index first, but when setting the parameter we give the index number second? Is this just "because", and I need to learn it that way? Or is there a particular justification for it? Not hard to wrap my head around, but curious as to a specific reason.

3 Answers

Steven Parker
Steven Parker
231,236 Points

The second argument to "enumerate" is optional. You can leave it off entirely unless you want the numbering to start with something other than 0. And it must come last because optional arguments must come after required ones.

Also, the terms are not quite equivalent. Each item in the output sequence contains an index and an item, but the arguments are an iterable and (optionally) an offset.

Jacob Truelove
Jacob Truelove
4,194 Points

Thank you for the answer! Makes a lot of sense. I am honored to have the Steven Parker answer one of my questions.

Steven Parker
Steven Parker
231,236 Points

I'm not sure why I'd get that "the" distinction, other than the fact that I answer questions often (so it's not so unlikely!). :wink:

Now for the Python area of the forum, "the" guy is Chris Freeman. :snake:

It makes sense that the second argument is optional, but why does it get assigned to the first variable? Wouldn't it normally be assigned in order?

Steven Parker
Steven Parker
231,236 Points

The order of the arguments is not related to the output. You always get 2-item tuples in the output even if you provide just one argument).

Euenlee Tan
Euenlee Tan
2,165 Points

Sorry, I still don't understand the reasoning. How does the system know to assign the "1" to "index" and not to item? I understand that if the code is: for item in rainbow, print (item,1), I get a similar result, which is, (1,"red"), (2,"orange").

Steven Parker
Steven Parker
231,236 Points

The optional second argument to the "enumerate" function is only used as the starting value of the index.

But your example isn't using "enumerate", and has a literal "1", so you should get a different output that looks like this:

red 1
orange 1

Euenlee Tan
Euenlee Tan
2,165 Points

I get that the second argument in "enumerate" is optional, and if not present starts from 0. However, consider the follow code: for index, item in enumerate(groceries, 1): print(f"{index}. {item}")

How does the system knows that, 1 is for index, and groceries is for item. Isn't this indirectly unpacking, if so, shouldn't it correspond to the order that it is placed?

I can accept it however that 1 is for index and groceries is for item, but just why?

Steven Parker
Steven Parker
231,236 Points

As I told Jacob, the order of the arguments is not related to the order of the output, that's just the way the function works. And the order of the output doesn't change whether you use the 2nd argument or not.

Keon Park
Keon Park
11,637 Points

I had the same question and I think I got it. When you use the enumerate() function, the output will be in the form of (index of element, element). So when you unpack this tuple with the variables set as index, item in the for loop, you are unpacking the index variable to the first element of the tuple and the item variable to the second element in the tuple.

This is not the correct answer, but I think a visualization might help.

'''python index, item = (index of element, element) ''' So I would say the order of the parameters in the enumerate functions do not correspond to the order of the inputs of the for loop.