List' object attribute 'sort' is read-only

Welcome to the Get Help category!
Hi, I am doing the Beginners Python course and I am trying to sort a 2D list, but keep getting the following error
list’ object attribute ‘sort’ is read-only. The list also does not sort, I can’t seem to figure out what is wrong. See my code below:

pizza_and_prices = [[2, “pepperoni”], [6, “pinapple”], [1, “cheese”], [3, “Sausage”], [2, “olives”], [7, “anchovies”], [2, “mushrooms”]]

print(pizza_and_prices)

pizza_and_prices.sort = ()

Hello.

You try to assign an empty tuple () to pizza_and_prices.sort attribute instead of call pizza_and_prices.sort() method. See the difference between assignment and function call.

Thanks, I did that, but the list is still not being sorted fro, ascending number, see below code

pizza_and_prices = [[2, “pepperoni”], [6, “pinapple”], [1, “cheese”], [3, “Sausage”], [2, “olives”], [7, “anchovies”], [2, “mushrooms”]]

print(pizza_and_prices)

pizza_and_prices.sort()

RETURNS
[[2, ‘pepperoni’], [6, ‘pinapple’], [1, ‘cheese’], [3, ‘Sausage’], [2, ‘olives’], [7, ‘anchovies’], [2, ‘mushrooms’]]

Hiya,
If your code is as you’ve written here, your print statement is before the sorting occurs.
Repeat your print statement after the .sort() line and it should show it’s changed.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.