Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Tuples Combo

igsm '
igsm '
10,440 Points

combo(), tuples and enumerate challenge. Please help.

Hi!

I do not understand how can we make a new tuple here that combines both iterables? Enumerate already outputs a tuple (index and value), as I understood - tuples are immutable. How can I use enumerate (index) here to build a new list of tuples that combine two iterables ? Thanks

zippy.py
# combo(['swallow', 'snake', 'parrot'], 'abc')
# Output:
# [('swallow', 'a'), ('snake', 'b'), ('parrot', 'c')]
# If you use list.append(), you'll want to pass it a tuple of new values.
# Using enumerate() here can save you a variable or two.
def combo(i1, i2):
    the_list = []
    for step in enumerate(i2):

3 Answers

igsm '
igsm '
10,440 Points

Sorry Kenneth, I am still little bit confused. I understand what output I should get but I cannot get how can I concatenate two iterables into the tulpe by using for loop and enamurate...

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Let's take what you have:

def combo(i1, i2):
    the_list = []
    for step in enumerate(i2):

Now, in your for loop, step is going to have two things in it. An index value for where we are in the loop (0 the first time, 1 the second, 2 the third, and so on) and the value of the loop. So looking at the example, the first time through your for loop, step is going to be (0, 'a') (enumerate() gives us tuples).

So how would you use step's contents to get the same indexed item out of i1? Maybe i1[step[0]]? It'd probably be easier if we unpacked the tuple from enumerate, huh? Let's change your code a bit.

def combo(i1, i2):
    the_list = []
    for index, value in enumerate(i2):

OK, now we have two variables that are named what the two things in the tuple are. So how would you use index to get the same indexed item out of i1? Again, probably i1[index], right? Now put i1[index] and value into a tuple and append that tuple to the_list. Then you're done!

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You have a good start.

Tuples are immutable, true, but they're only immutable once they've been created. You can, of course, put into them whatever you'd like. So for each step of your for loop, you make a new tuple w/ two items in it, one from each iterable, and then put that tuple into your the_list variable. No tuples will be harmed in the running of this loop!

igsm '
igsm '
10,440 Points

Ok. Thanks Kenneth. I got it now.