How to do index when you zipped and sorted in acending

Your code below:

toppings=[“pepperoni”, “pineapple”, “cheese”, “sausage”, “olives”, “anchovies”, “mushrooms”]

prices=[2, 6, 1, 3, 2, 7, 2]




#print(“we sell”,(num_pizzas),“different kinds of pizza!”)



print (sorted(pizza_and_prices,key=lambda x:x[0]))

#lambda for special sorted

I don’t quite understand your question.
Your code seems to work the way it is.
What are you trying to do next?

toppings = ["pepperoni", "pineapple", "cheese", "sausage", "olives", "anchovies", "mushrooms"] prices = [2, 6, 1, 3, 2, 7, 2] pizza_and_prices = zip(prices,toppings) print( sorted(pizza_and_prices, key=lambda x:x[0]) ) pizza_and_prices = zip(prices,toppings) in_order = sorted(pizza_and_prices, key=lambda x:x[0]) #print( list(zip(*in_order))[1] ) print( [pairs[1] for pairs in in_order] )

Are you referring to step 12 where you have to insert the new topping [2.5, "peppers"] at index 4?

I try to do
pizza_and_prices[-1] to select the last element
but the system is giving me :

TypeError: ‘zip’ object is not subscriptable.

sorry about the confusion

you could do
pizza_and_prices = zip(prices,toppings)
the last element can be accessed as
last = list(pizza_and_prices)[-1]

an another way to do that is:

pizza_and_prices = list( zip(prices,toppings) )
last = pizza_and_prices[-1]

you have to make the zip object into a list (or something else) to be able to access parts of it

The second example is an appropriate way to do it since the first consumes the zip object with list() but the list itself is not preserved since only the last element was assigned.

To see this played out,

pizza_and_prices = zip(prices, toppings)
last = list(pizza_and_prices)[-1]
print (list(pizza_and_prices))    # should raise an error
1 Like