I have a shopping list:
items = [
['Aspirin', 'Walgreens', 6.00],
['book lamp', 'Amazon', 2.87],
['popsicles', 'Walmart', 5.64],
['hair brush', 'Amazon', 6.58],
['Listerine', 'Walmart', 3.95],
['gift bag', 'Target', 1.50]
]
I want to sort the items from cheapest to highest price, and remove the prices. (I don't need them anymore then, I'll just buy from top down until I run out of money). Goal is:
items = [
['gift bag', 'Target'],
['book lamp', 'Amazon'],
['Listerine', 'Walmart'],
['popsicles', 'Walmart'],
['Aspirin', 'Walgreens'],
['hair brush', 'Amazon']
]
A way that works but looks clumsy (demo/template):
import operator
items = sorted(items, key=operator.itemgetter(2))
for i in range(len(items)):
items[i] = items[i][:2]
Is there a shorter way?