"Lens slice" .pop trouble

I’m having trouble getting the syntax right on the .pop function I keep playing with placement and bracketing thinking I’m just doing something wrong but to no avail so this is my first post in the forums. So below is the relavent chunk. In my first thought. “.pop” would just grab the first (and only) “anchovies” it finds, starting from the end. no dice. below the main chunk of code are a few interations i tried attempting to the the syntax to push through

# Checkpoint 6

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

# Checkpoint 7

#print(pizza_and_prices)

# Checkpoint 8

pizza_and_prices.sort()

print(pizza_and_prices)

# Checkpoint 9

cheapest_pizza = pizza_and_prices[0]

print(cheapest_pizza)

# Checkpoint 10

priciest_pizza = pizza_and_prices[-1]

print(priciest_pizza)

# Checkpoint 11

pizza_and_prices.pop("anchovies")

pizza_and_prices.pop(-1)

pizza_and_prices[-1].pop(-1)

pizza_and_prices[-1].pop("anchovies")

Several others too. With no “anchovies” involved in the line it at least doesnt error code but it also doesnt nix the anchovies from the list. I’m sure this is something quite simple and just messing up. TIA
-Chris

No need to supply an argument with .pop() b/c it automatically removes the last item if no index is supplied. Or, I guess you could have written:

pizza_and_prices.pop(-1)
print(pizza_and_prices)
[7, 'anchovies']
[[1, 'cheese'], [2, 'mushrooms'], [2, 'pepperoni'], [3, 'olives'], [3, 'sausage'], [6, 'pineapple']]

See:
https://docs.python.org/3/tutorial/datastructures.html

I Just figured it out. Kind by accident. I had in fact done it right mulitple times prior but i never re printed and therfore did not see the changes that i was succefully making. Thanks for your input tough spot on.

1 Like

print() is your friend when de-bugging code.
:slight_smile:

1 Like