Python Python Collections (2016, retired 2019) Tuples Combo

Alexandru Patriche
Alexandru Patriche
10,457 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,441 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,457 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!