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

Alexandru Patriche
Alexandru Patriche
10,466 Points

Tuples, Combo Challenge advice

Hi Treehouse,

I wrote the code below and when I test it locally it works but not perfectly. Calling combo([1, 2, 3], 'abc') returns only [(2, 'b')]. I take it as I'm relatively on the correct path but could use some advice, please.

Thank you!

combo.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]

def combo(iterable1, iterable2):
    tuple_output = []
    x=0
    for iterable in iterable1, iterable2:
         tuple_output = [(iterable1[x], iterable2[x])]
         x+=1
    return tuple_output

1 Answer

Eduardo Valencia
Eduardo Valencia
12,444 Points

Hey. This is because you are setting the tuple_output equal to the new tuple you generate in your for loop in lieu of appending it to the tuple_output. Consequently, it will always be set to the last generated tuple in your for loop. To fix this, simply append a tuple using the .append() method in lieu of setting it equal to an array with a tuple.

Alexandru Patriche
Alexandru Patriche
10,466 Points

Thanks for your answer, Eduardo! I have updated the code to be tuple_output.append([(iterable1[x], iterable2[x])]). Now the result is (1, a), (2, b), but no (3, c) yet. I'm working on it but at least I am on the right path now. Thanks!