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

Raduica Sebastian
PLUS
Raduica Sebastian
Courses Plus Student 1,356 Points

Need help with combo!

Can someone please tell me why this isn't working? thank you!!

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

def combo(iterable1,iterable2):
    list_of_tuples=[]
    for item in iterable1:
        for item1 in iterable2:
            tuple1=item,item1
            list_of_tuples.append(tuple1)
    return list_of_tuples

2 Answers

Gabbie Metheny
Gabbie Metheny
33,778 Points

First off, you should only need one for loop to iterate over both iterables.

for item in iterable1:
    # do something with iterable1 and iterable2

This will work because both iterables are the same length, so the loop will run as many times as there are items in either iterable.

When looping over the two iterables, you need a way to keep track of what index you're at on each loop. One solution would be to set up a counter variable before the loop. Then you can use that variable as the index number for iterable1 and iterable2, incrementing the counter by one at the end of each time through the loop.

Let me know if you need further clarification!