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 (Retired) Tuples Combo

I can't understand why this wont pass

I've been looking at the python documentation for .zip()

And if I write this script

def combo(x, y):
    zipped = zip(x, y)
    print(zipped)

x = [1, 2, 3]
y = "abc"
combo(x, y)

and run it in Terminal i get this output:

$ python zippy.py 
[(1, 'a'), (2, 'b'), (3, 'c')]

Looks pretty correct to me, but why wont it pass in Workspaces?

zippy.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
# If you use .append(), you'll want to pass it a tuple of new values.
def combo(x, y):
    zipped = zip(x, y)
    return zipped

Just for future reference. A solution for using zip() in Python 3:

def combo(x, y):
    output = list(zip(x, y))
    return output

3 Answers

Ryan S
Ryan S
27,276 Points

Just looking at the zip docs, I think the issue is that the zip() function is a generator, so just assigning it to a variable will not actually call it like a regular function. In order to make use of a generator you'd need to call it in a loop of some kind. If you return list(zipped) it seems to work in the code challenge. However, I'm not sure why it worked for you in the terminal using print(). I can't seem to get it to work that way, it just returns the memory location of "zipped", which is what you'd expect with a generator.

It works in Python2:

$ python zippy.py 
[(1, 'a'), (2, 'b'), (3, 'c')]

But not in Python 3:

python zippy.py 
<zip object at 0x102073248>

And yeah, the documentation is also different. I didn't realize I was reading the Python 2 documentation.

Ryan S
Ryan S
27,276 Points

Ah that makes sense!

The problem is that you are printing the zipped variable, and not returning it.

Try returning zipped instead of printing it :)

Good luck! ~alex

Yeah, true I printed it to the screen in the script I ran in the Terminal so that I could see and verify the correct output. But If you look at the zippy.py (the last code) imported from Workspaces. I return zipped.

def combo(x, y):
    zipped = zip(x, y)
    return zipped

Oooooh.. Didn't see that :)

What's the error?

Uhm, something like. Bummer :D

That's all it says?