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 (2016, retired 2019) Tuples Combo

Deniz Kahriman
Deniz Kahriman
8,538 Points

How can I fix the code below? Trying to combine 2 iterables in tuples within a list. Thank you!

I was also thinking of using "enumerate" but not sure how to proceed. Thanks in advance!

combo.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(iterable1, iterable2):
    my_list = []
    for i in iterable1 and iterable2:
        my_list.extend((iterable1[i], iterable2[i]))
return my_list

1 Answer

Steven Parker
Steven Parker
229,644 Points

You can't combine two iterables with "and" in a for loop.

But they did say you can assume both are the same length. Perhaps that might help you construct a loop.

Deniz Kahriman
Deniz Kahriman
8,538 Points

Hey Steven, I changed my approach but not sure if I'm in the right direction.

def combo(x,y):
    listing = []
    i = 0
    while True:
        listing[i] = (x[i],y[i])
        i += 1
    return listing
Steven Parker
Steven Parker
229,644 Points

It looks like you have a good idea there, but you don't want your loop to run forever ("while True"). Instead of "True" use a conditional expression that will cause the loop to stop after it does all the items. Also, lists have a method for adding new items that might work better than assigning using an index.