Zip() function shows no output in the online terminal

Python 3 course - Module 3 (Lists) - Len’s Slice (Project) - Task 6

We are asked to combine two lists into a single 2 dimensional list.
Upon seeing that this would mean manually typing a list containing 14 elements, I nope out of there and research a better method. Apparently, the zip() function does exactly that, although it was never introduced in the course.

When printing, though, the terminal does not seem to like it. It outputs this: <zip object at 0x7fcb3fd29d08>

Screenshot: https://i.imgur.com/YzClTDn.png

Link: https://www.codecademy.com/courses/learn-python-3/projects/python-lens-slice

Did I do something wrong, or…?

Please post a link to this exercise. Thanks.

Not sure if the zip() function is to be implemented, or if we are just supposed to compose the list, manually. Will need to review the instructions.

Here is an algo that emulates what zip does when given two lists.

>>> a = [2, 3, 4, 5, 6, 7, 8, 9]
>>> b = [4, 9, 16, 25, 36, 49, 64, 81]
>>> c = []
>>> for i in range(len(a)):
	c.append((a[i], b[i]))

	
>>> c
[(2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, 64), (9, 81)]
>>> 
3 Likes

Thank you for your answer. That has definitely not been taught by this point in the course so I imagine they just want us to type the whole thing manually, I just wanted to see what better alternatives existed. This is great, thanks. Being able to use zip() would be great though, as it is really simple and efficient.

The link to the project is: https://www.codecademy.com/courses/learn-python-3/projects/python-lens-slice

Hey Ignasivelasco,

The zip function returns a zip object that is why you are seeing <zip object at 0x7fcb3fd29d08> . If you wanted to print the values of a zip object, you can convert it into a list object and then print it. Regarding your code, try using the list() function to convert the zip object :

pizza_and_prices = list(zip(price, toppings))

which will give you a list of tuples combining prices and toppings like so:

[(2, 'pepperoni'), (6, 'pineapple'), (1, 'cheese'), (3, 'sausage'), (2, 'olives'), (7, 'anchovies'), (2, 'mushrooms')]

But as MTF mentioned, you may not need to implement the zip() function to negotiate the task. I hope this helps you out!

2 Likes

From the example it looks we are asked to build a list of lists. We can construct it manually, or use the algo from above.

pizza_and_prices = []
for i in range(len(prices)):
	pizza_and_prices.append([prices[i], toppings[i]])
pizza_and_prices.sort()
1 Like

Thank you very much both! My code worked after placing the zip inside a list function as seraph suggested :slight_smile:

Edit for clarity, this solved it:
pizza_and_prices = list(zip(price, toppings))

1 Like

Their is no suggestion of using zip, so it should not be used. Besides, it generates a list of tuples, not a list of lists, as given in the instruction example.

This exercise was not intended to teach about zip(). Following instructions is a very important skill to develop.

1 Like

Alright, noted. Since it was part of a self-assessed project rather than a lesson I thought I would take the freedom to tackle it in a more interesting way than manually typing a list. But yours is a fair point, I am now learning about tuples and see why this is an issue.

1 Like