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 trialcsj
12,678 PointsI 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?
# 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
3 Answers
Ryan S
27,276 PointsJust 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.
Ryan S
27,276 PointsAh that makes sense!
Alexander Davison
65,469 PointsThe problem is that you are printing the zipped
variable, and not returning it.
Try returning zipped
instead of printing it :)
Good luck! ~alex
csj
12,678 PointsYeah, 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
Alexander Davison
65,469 PointsOooooh.. Didn't see that :)
Alexander Davison
65,469 PointsWhat's the error?
csj
12,678 PointsUhm, something like. Bummer :D
Alexander Davison
65,469 PointsThat's all it says?
csj
12,678 Pointscsj
12,678 PointsJust for future reference. A solution for using
zip()
in Python 3: