Error when sorting a list of lists created with zip


#1

Unit 3: Zip two lists together, then sort based on sublist.

code:

toppings = ['pepperoni', 'pineapple', 'cheese', 'sausage', 'olives', 'anchovies', 'mushrooms']

prices = [2, 6, 1, 3, 2, 7, 2]

pizzas = zip(prices,toppings)

pizzas.sort()

print(pizzas)

error:
Traceback (most recent call last):
File “script.py”, line 12, in
pizzas.sort
AttributeError: ‘zip’ object has no attribute ‘sort’


#2

Can you provide the exercise url? I need to have a look at the exercise


#3

Any solution? I am having the same error.


#4

You could have provided me with the exercise url so i could help you further? But no, you decided to make a reply which is not helping us forward

there are no magic fixes, so if you want help, please make a better question/provide what i asked for

i know i am harsh now, but new learners need to learn that in order to get good help, you need to ask good question/provide the needed information

so if you can provide what i need, i am sure i can help you further :slight_smile:


#6

Sorry. I assumed you were in contact with OP maybe through private message or something.

In “Lens Slice” project #7: https://www.codecademy.com/paths/data-science/tracks/dspath-python-lists-and-loops/modules/dspath-lists/projects/lens-slice

Also had a zip error on “Python Gradebook” #7: https://www.codecademy.com/paths/data-science/tracks/dspath-python-lists-and-loops/modules/dspath-lists/projects/gradebook


#7

Hm… maybe this course was original designed for python2? Because in python2 zip() produces a list, then calling sort is perfectly fine.

however, in python3 zip() produces an zip object (which gives better performance) but a zip object does not have a .sort() method, then you would need to convert to list first.

you can use list() to achieve this


#9

even if i hadn’t included how to do the conversion, you know now the problem, the fix what needs to be applied, so surely you can google it? Learning to google, reading documentation and stackoverflow answers are important skills as programmers


#10

pizzas_zipped = zip(prices, toppings)

pizzas = list(pizzas_zipped)

print(pizzas)

pizzas.sorted()

I am still getting “AttributeError: ‘list’ object has no attribute ‘sorted’”. Isn’t the zip converted to a list now?


#11

its is a list, you should see that with your print. but you confuse the list .sort() method with the sorted() function. Which is why it isn’t working.


#12

sorted returns a sorted list that points to the same values, but does not alter the orginal list.

>>> a = [8, 6, 3, 8, 2, 4, 3, 6, 9, 1]
>>> b = sorted(a)
>>> a
[8, 6, 3, 8, 2, 4, 3, 6, 9, 1]
>>> b
[1, 2, 3, 3, 4, 6, 6, 8, 8, 9]
>>> 

sort(), on the other hand, replaces the original list with the sorted one.

>>> a.sort()
>>> a
[1, 2, 3, 3, 4, 6, 6, 8, 8, 9]
>>> 

Let’s see this played out with a list of tuples…

>>> a = [(7, 1), (8, 2), (5, 3), (6, 4)]
>>> a.sort()
>>> a
[(5, 3), (6, 4), (7, 1), (8, 2)]
>>> 

We can use a key getter to sort by item in the tuple but that means writing one, or we can draw of the C library and let Python handle it.

>>> from operator import itemgetter
>>> a.sort(key=itemgetter(1))
>>> a
[(7, 1), (8, 2), (5, 3), (6, 4)]
>>> 

Using sorted is the way to go if we want to preserve the orginal list.

>>> toppings = ['pepperoni', 'pineapple', 'cheese', 'sausage', 'olives', 'anchovies', 'mushrooms']
>>> prices = [2, 6, 1, 3, 2, 7, 2]
>>> z = zip(toppings, prices)
>>> z
<zip object at 0x02EA24B8>
>>> u = list(z)
>>> u
[('pepperoni', 2), ('pineapple', 6), ('cheese', 1), ('sausage', 3), ('olives', 2), ('anchovies', 7), ('mushrooms', 2)]
>>> v = sorted(u)
>>> v
[('anchovies', 7), ('cheese', 1), ('mushrooms', 2), ('olives', 2), ('pepperoni', 2), ('pineapple', 6), ('sausage', 3)]
>>> from operator import itemgetter
>>> v = sorted(u, key=itemgetter(1))
>>> v
[('cheese', 1), ('pepperoni', 2), ('olives', 2), ('mushrooms', 2), ('sausage', 3), ('pineapple', 6), ('anchovies', 7)]
>>>