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 trialAlexandru Patriche
10,466 PointsTuples, 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([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
12,444 PointsHey. 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
10,466 PointsAlexandru Patriche
10,466 PointsThanks 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!