Mona Jalal Mona Jalal - 6 months ago 9
Python Question

didn't get the right output for creating list of tuples given two lists

I am following TreeHouse tuple tutorials for Python 3 and I got this error for the following code:
enter image description here

def combo(iter1, iter2):
ltup = []
for a in iter1:
for b in iter2:
ltup.append(tuple([a, b]))
return ltup


enter image description here

How should I fix it and why is my solution wrong?

I have used
zip
and it didn't pass the challenge:
enter image description here

Answer

You do not need nested cycles. Try this code:

def combo(iter1, iter2):
    ltup = []
    for a,b in zip(iter1, iter2):
            ltup.append((a, b))
    return ltup

print combo([1,2,3],[3,2,1])

Or another the shortest variant, I guess: zip(iter1, iter2)

[(1, 3), (2, 2), (3, 1)]