How to sort second element in sublist?

Here is the code :

pizza_and_prices = [['pepperoni',2],['pineapple',6],['cheese',1],['sausage',3],['olives',2],['achovies',7],['mushroom',2]]

i want to sort the numbers 2,6,1,3,2,7,2 (second element of the sublist) in ascending order. How can i achieve this?

pizza_and_prices.sort(key=lambda x: x[1])

1 Like

More information on the .sort() method here:
https://docs.python.org/3/howto/sorting.html

1 Like