Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,296 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
221,296 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.