Error when sorting a list of lists created with zip

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’

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

Any solution? I am having the same error.

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:

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

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

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

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?

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.

1 Like

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)]
>>> 

Ok so you went forward now just reverse the process. I am not a seasoned programmer but it makes sense and I suggest printing every step to see the outcome.

subjects.append("computer science")
grades.append(100)

print(list(subjects))
print(list(grades))

gradebook = zip(subjects, grades)
print(gradebook)

REMEMBER you went forward, you created a “zip” list. Now create a list out of the zip.

gradebook = list(zip(gradebook))
gradebook.append(("visual arts", 93))

print(gradebook)

full_gradebook = gradebook + last_semester_gradebook

print(list(full_gradebook))

Here is how I did it, remember variables overwrite variables. In this case you can see it go forward and backward if you look closely. You are essentially placeing lists into a zip to then bring them back out into a list to .append them.

3 Likes

Think of .sort the same way.

convert zip into list and then use sorted function
ex. pizzas = sorted(list(zip(prices,toppings)))

3 Likes

Problem # 5 on Len’s Slice. List practice chapter, Part 2.
I am also now stuck on this, although the previous problem. Same quiz.

Traceback (most recent call last):
File “script.py”, line 7, in
pizza_and_prices = [[2, “pepperoni”][6, “pineapple”][1, “cheese”][3, “sausage”][2, “olives”][7, “anchovies”][2, “mushrooms”]]
TypeError: list indices must be integers or slices, not tuple
I have no idea what I’m doing wrong.

this line:

 [[2, "pepperoni"][6, "pineapple"][1, "cheese"][3, "sausage"][2, "olives"][7, "anchovies"][2, "mushrooms"]]

worries me a lot, missing comma’s to separate the elements in your outer list

Yeah, I got it yesterday. Thank you.