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 trialKristinn Godfrey
6,161 PointsAt 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
Treehouse Moderator 68,457 PointsThe 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.
Jude Molloy
7,470 PointsChris you answer every question I search, you should become a treehouse teacher! :)
Kristinn Godfrey
6,161 PointsKristinn Godfrey
6,161 PointsHey! 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
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsPerhaps we are thing of "size" differently. I am thinking that "size" is similar length of the item.
In Kenneth's code:
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 casemy_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 variablestep
.The "star" notation
*step
means to expandstep
into a it's individual members.*step
is shorthand forstep[0], step[1]
Kristinn Godfrey
6,161 PointsKristinn Godfrey
6,161 PointsThank 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.