Python Python Collections (2016, retired 2019) Tuples Combo

Sohail Mirza
Sohail Mirza
Python Web Development Techdegree Student 5,158 Points

Why doesnt my code work

i do get the required output but challenge fails it. What am i missing?
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(iterable1, iterable2):
    output = []
    for item in iterable1:
        for item1 in iterable2:
            output.append((item, item1)),
    return output

print(combo("five", "four"))

2 Answers

Steven Parker
Steven Parker
203,990 Points

The challenge shows a test example of "combo([1, 2, 3], 'abc')", and the correct output shown is:
[(1, 'a'), (2, 'b'), (3, 'c')]
But for those arguments this code outputs:
[(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c')]

For another example, I see you're testing with the strings "five" and "four". The correct output should be:
[('f', 'f'), ('i', 'o'), ('v', 'u'), ('e', 'r')]

But with those arguments this code produces:
[('f', 'f'), ('f', 'o'), ('f', 'u'), ('f', 'r'), ('i', 'f'), ('i', 'o'), ('i', 'u'), ('i', 'r'),
    ('v', 'f'), ('v', 'o'), ('v', 'u'), ('v', 'r'), ('e', 'f'), ('e', 'o'), ('e', 'u'), ('e', 'r')]

So give it another shot. And for the challenge, you won't need to call the function yourself or print anything.

Steven Parker
Steven Parker
203,990 Points

The challenge wants the function to pair up items from both iterables on a one-to-one basis. So in the output, the number of tuples would be the same as the length of either iterable (it tells us they will be the same).

But the code shown here uses a nested loop to pair each item from the first iterable with every item in the second. So, as you see in the examples, you get a much larger output.

So instead of nested loops, try implementing a way to step through both iterables together in one loop to create the output.