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 Collections/Tuples/Tuples with functions

I don't understand why this prints out the whole alphabet, instead of just printing out the first 2 letters over and over again:

for step in enumerate(my_alphabet_list):

print('{}, {}'..format(step[0],step[1]))

2 Answers

Hi Elizabeth, in a for loop, once the instructions have been followed, it moves on to the next item.

As an analogy, imagine you're handed a stack of papers and told that you need to write your name on each one. If you're like Python, you'll write your name on the first paper, and then set it aside, and then write your name on the second paper, and then set it aside, and so on, for as long as you have papers. Once you've hit the end of the pile, you'll know that your job is complete - and the final result will be a stack of papers where each one has your name written just once (instead of one paper with your name written 20 times!).

Hope that helps!

Yes, I get that, but what I find confusing about this is that it keeps using step[0] and step[1]. I am feeling like it should go to step[26]. Maybe step keeps getting rewritten, with step[0] as the index and step[1] as the letter in the alphabet. So does that mean that when you use enumerate, the thing you are looping over (step) is no longer a single variable, but is now a list with 2 items, at index [0] and [1]?

Ah! Each step in the enumerate returns a tuple of the form (number, letter_string), e.g., (1, 'a'), (2, 'b')... and so on.

step[0] then calls the value from the first position of the tuple (the number) and step[1] calls the value from the second position of the tuple (the letter string).

Does this help?

Yes, got it now, thanks!