-2

I am trying to put a list of groceries into python and then ranking them with the lowest price being first. Does anyone know how I could display this so the result would look like:

Popcorn: 1.23
Eggs: 3.77
Water: 4.34
Milk: 7.12
Toast: 8.45

This is what I have tried so far

price_list = [["Eggs", "3.77"], ["Toast", "8.45"], ["Milk", "7.12"], ["Water", "4.34"], ["Popcorn", "1.23"]]
print("Original order: ", price_list)
price_list.sort()
print("Sorted Order: ", price_list)
aydow
  • 3,673
  • 2
  • 23
  • 40

1 Answers1

1
price_list = [["Eggs", "3.77"], ["Toast", "8.45"], ["Milk", "7.12"], ["Water", "4.34"], ["Popcorn", "1.23"]]
print("Original order: ", price_list)
print("Sorted Order: ", sorted(price_list, key=lambda x: float(x[1])))
Harry Moreno
  • 10,231
  • 7
  • 64
  • 116