Learn Python 3 - Len's Slice - Inserting Elements into 2D List at Specific Index

Hey All!

I couldn’t find the answer to this anywhere, nor any code examples to relate to.
I am stuck on Learn Python 3 - Len’s Slice - Q12:

Since there is no longer an "anchovies" pizza, you want to add a new topping called "peppers" to keep your customers excited about new toppings. Here is what your new topping looks like:
* *[2.5, "peppers"]* *
Add the new peppers pizza topping to our list pizza_and_prices .

Note: Make sure to position it relative to the rest of the sorted data in pizza_and_prices , otherwise our data will not be correctly sorted anymore!

I am struggling to adhere to inserting at both the elements, what I have at present is:

toppings = ["pepperoni", "pineapple", "cheese", "sausage", "olives", "anchovies", "mushrooms"] prices = [2, 6, 1, 3, 2, 7, 2] num_two_dollar_slices = prices.count(2) print(num_two_dollar_slices) num_pizzas = len(toppings) print(num_pizzas) print("We sell " + str(num_pizzas) + " different kinds of pizza!:") print() pizza_and_prices = list(zip(prices, toppings)) print(pizza_and_prices) pizza_and_prices.sort() print() cheapest_pizza = pizza_and_prices[0] print("Our cheapest on the menu is " + str(cheapest_pizza)) priciest_pizza = pizza_and_prices[-1] print("Our priciest on the menu is " + str(priciest_pizza)) print() print("Oh no! Somebody just grabbed the last slice of anchovies") pizza_and_prices.pop(-1) print() print("We do have the following options left though... and I am also baking some fresh peppers now: " + str(pizza_and_prices)) print() pizza_and_prices.insert(2.5, "peppers") print("Updated Menu:") print(pizza_and_prices)

My problem is at “pizza_and_prices.insert(2.5, “peppers”)” in which fails as my inner list is composed of integers and not floats: (do I have to manipulate the value, similar to “str”?)

Traceback (most recent call last):
** File “script.py”, line 24, in **
** pizza_and_prices.insert(2.5, “peppers”)**
TypeError: integer argument expected, got float

If I change it to an Int, it does index at the correct value but I am not sure how to insert both arguments with an index of 2.


From: 5. Data Structures — Python 3.9.6 documentation

list. insert ( i , x )

Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x) .

So it format is list.insert(index, object). It must have an integer index (within the the range of the length of the list). The object can, however, be something else (a list, a tuple, a dictionary, an int, a float, etc.)


Looking at your data, the list is composed of tuples, so be sure to insert an explicit tuple. @toastedpitabread gives a good enough hint to how to complete this.

1 Like


Thanks very much for the quick response.
So if I use:

pizza_and_prices.insert(2, “peppers”)

That inserts at index 2 which is what I want since it needs to be sorted.
But how do I insert 2.5 as a float and peppers as a string at index 2?
Attempting to look at Stack Overflow of explicitly defining a tuple and miserably failing on this one.


Be sure to match your other data entries… Tuples.

pizza_and_prices.insert(2, (2.5, “peppers”))
                            ---- tuple ----

Thanks very much, I see know!

1 Like

I’m literally on the same problem, thank god someone else had the same issue! Thank you!

1 Like