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

Michael Pastran
Michael Pastran
4,727 Points

I have a question regarding the part of the video on tuples

okay, so in the part where kenneth writes

for step in my_vocab_list():
    print ("{} : {}".format(step[0],step[1])

can someone explain where the [0] and [1] come from? and what does it mean? we get the same result but we are passing two indexes. wouldnt it just print the same thing over and over again. printing out whatver is at index[0] and index[1]??

2 Answers

Jon Peterson
Jon Peterson
12,661 Points

my_vocab_list is a list of tuples, step is each tuple in the list. so it will print out first_tuple[0] , first_tuple[1]... then second_tuple[0], second_tuple[1]... and so on for as many tuples are in your my_vocab_list

Joshua Lawson
Joshua Lawson
4,953 Points

I too, am confused.

So first_tuple[0] would be the count (1), and first_tuple[1] would be (a)?
second_tuple[0] would be (2) and second_tuple[1] would be (b)?

Nathan McElwain
Nathan McElwain
4,575 Points

Tuples are iterable, like strings and lists. The [0], [1] are indexes, or positions, of the contents of the iterable, just like you would when iterating through a list or string. Like Jon said, "step" is the simply the variable used to iterate through "my_vocab_list", which is the iterable.