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

santosh kumar peddineni
seal-mask
.a{fill-rule:evenodd;}techdegree
santosh kumar peddineni
Python Web Development Techdegree Student 2,661 Points

can someone help me with the code below?

i am not sure where i am getting wrong in the logic i wrote can someone guide me with the correct logic.

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

def combo(x,y):
    output = []

    for i, v in enumerate(x):
        for j, u in enumerate(y):
            if i == j:
                tup = v,u
            output.append(tup)
            j += 1
        i +=1
    return output

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Have you tried running your code adding print statements? Once you see your results, it will help you debug your code. It works for me with 1 line indentation change. I also think you have two lines that are unnecessary (but don't seem to hurt anything).

By putting in debug statements similar to below will help you see what's happening with your logic:

# print statement after function to call the function
print(combo([1, 2, 3], 'abc'))

# print statement in the inner loop - can also add other info if desired
print("inside inner loop" + str(j))

# print statement in the outer loop - can also add other info if desired
print("inside outer loop" + str(i))
Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Santosh,

You don't need to use a loop inside a loop. You can create a single loop that goes from 0 to the length of the lists and then inside that loop, during each iteration you get the element from each list at that index, combine into a tuple and append to your list.

Alternatively, you can use the zip function (update: good catch Dave varmutant , while zip is the best solution to this problem if you find it in the wild, it is forbidden by the code checker in this particular case).

Cheers

Alex

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Yep, many ways to code this challenge but don't try zip() - I tried it and it won't pass the challenge saying to work it out without zip(). :smile: