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 (Retired) Tuples Tuples With Functions

At 3:00 why does step[0] and step[1] iterate to next tuple item?

Aren't step[0] and step[1] fixed sizes?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The bracket notation designates the index into a container, such as as a list or a string. It does not have to do with the size of step. Since enumerate creates a tuple with length 2, step[0] is the zero-th indexed item or first item, and step[1] is the 1-index item or second item.

Hey! Thanks for answering, you answered my previous question 5 minutes ago.

You say: "It does not have to do with the size of step". Why doesn't it? I don't see why the outcome isn't just: ['0: a'], ['0: a'], ['0: a'] etc... like my previous problem was. What makes enumerate iterate through the whole list? I'm sorry I don't get it.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Perhaps we are thing of "size" differently. I am thinking that "size" is similar length of the item.

# tuple of length ("size") 2
(0, 'a')
# tuple of length ("size") 4
(0, 1, 2, 3)
# list of length  ("size") 3
['a', 'c', 'e']

In Kenneth's code:

for step in enumerate(my_alphabet_list):
    print("{}: {}".format(*step)

enumerate keeps track of the number of iterations and in each loop, it returns the iteration count and the next item from the iterable, in this case my_alphabet_list.

For the first iteration, enumerate returns (0, a) On the next iteration, it returns (1, b), next it returns (2, c), etc.

On each iteration the tuple returned by enumerate is assigned to the variable step.

The "star" notation *step means to expand step into a it's individual members.

*step is shorthand for step[0], step[1]

Thank you very much. I understand it now.

It's that this example is about simply formatting the output. But the last example was about appending to the variable.

Chris you answer every question I search, you should become a treehouse teacher! :)