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

I have a slight issue with a line of code in my project> Specifically task 8 or line 12 . I cannot get the syntax right to order the pizzas in ascending price aka from cheapest to the most expensive. Any advice in the matter would be appreciated.

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

Sorting and Slicing Pizzas

Sort pizza_and_prices so that the pizzas are in the order of increasing price (ascending).

I think

pizza_and_prices.sort("str") 

should be

pizza_and_prices.sort() 

for ascending order (least-to-greatest)

or

pizza_and_prices.sort(reverse=True) 

for descending order (greatest-to-least)
[reverse is the keyword there]

1 Like

I looked at the code again(line12) and I realized that it isn’t a list but a tuple containing two different data types. integers and strings so the sort command doesn’t execute

| janbazant1107978602
March 5 |

  • | - |

I think

pizza_and_prices.sort("str") 

should be

pizza_and_prices.sort() 

for ascending order (least-to-greatest)

or

pizza_and_prices.sort(reverse=True) 

for descending order (greatest-to-least)
[reverse is the keyword there]

I think the list contains two different types of data instead of one type in this case price and name of pizza that might explain why the list is not sorted once

pizzas_and_prices.sort() is applied . There was no reference to these kinds of list previously.

| janbazant1107978602
March 5 |

  • | - |

I think

pizza_and_prices.sort("str") 

should be

pizza_and_prices.sort() 

for ascending order (least-to-greatest)

or

pizza_and_prices.sort(reverse=True) 

for descending order (greatest-to-least)
[reverse is the keyword there]

Shouldn’t pizza_and_prices be a 2D list (meaning a list of lists) ?

like this:

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

or you could do:

pizza_and_prices = list(zip( prices, toppings ))

In my project, I used a list comprehension

n = len(pizza)
pizza_and_prices = [ [prices[i], toppings[i]] for i in range(n) ]

In those situations, .sort() should work.

I changed the original list into a 2d list using the example you sent. Using all square brackets. It returned a syntax error on line 10.

| janbazant1107978602
March 6 |

  • | - |

Shouldn’t pizza_and_prices be a 2D list (meaning a list of lists) ?

like this:

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

or you could do:

pizza_and_prices = list(zip( prices, toppings ))

In those situations, .sort() should work.

you have an extra ] in that list, here:
[]2, "olives"]

2 Likes