Https://www.codecademy.com/courses/learn-python-3/projects/python-lens-slice

https://www.codecademy.com/courses/learn-python-3/projects/python-lens-slice

The instructions are:
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!

Here’s what I did:
39. pizza_and_prices.insert(int(2.5), “peppers”)

When I print it shows:
[(1, ‘cheese’), (2, ‘mushrooms’), ‘peppers’, (2, ‘olives’), (2, ‘pepperoni’), (3, ‘sausage’), (6, ‘pineapple’)]
The 2.5 is missing. no error reported. How can I get the 2.5 to print?

The insert method is designed to insert a new element at a specific index- seq.insert(index, element) https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types

So the result of seq.insert(int(2.5), "peppers") is to insert the string “peppers” at index 2 (the result of calling int with 2.5 is an integer 2).

You’d need to adjust your code to insert at the index you needed with an element that is that list you specified, [2.5, "peppers"].

Ahh. That makes sense. I was forgetting the index location and not placing the data inside brackets.


it worked. Thanks.

1 Like

I am using -
pizza_and_prices.insert(4, “2.5, peppers”)

o/p
[[1, ‘cheese’], [2, ‘mushrooms’], [2, ‘olives’], [2, ‘pepperoni’], ‘2.5, peppers’, [3, ‘sausage’], [6, ‘pineapple’]]

it does not look like other items in the list. How do we use inser() with 2D list ?
Iam definetly missing something obvious here, please help!

I am using -
pizza_and_prices.insert(4, “2.5, peppers”)

o/p
[[1, ‘cheese’], [2, ‘mushrooms’], [2, ‘olives’], [2, ‘pepperoni’], ‘2.5, peppers’, [3, ‘sausage’], [6, ‘pineapple’]]

it does not look like other items in the list. How do we use inser() with 2D list ?
Iam definetly missing something obvious here, please help!

The second argument to the insert method is what you insert into your list. You pass "2.5, peppers" which is a string. How would you create a list with those two elements? Once you’ve got that bit pass it instead of the string.

1 Like

pizza_and_prices.insert(4, [2.5, “peppers”]) ----> this worked

3 Likes

did not work for me!

i copy pasted the code you mentioned. What can be wrong?

the following error is generated:

File “script.py”, line 30
pizza_and_prices.insert(4, [2.5, “peppers”])
^
SyntaxError: invalid character in identifier

I got it. Thank you. So, when I copy pasted, I think, Python differentiated between inverted commas being straight or curved.

They are indeed different characters with the trick being that the forum automatically alters the standard " to a “ which is troublesome for code as it also affects other characters such as * and ` for example. If you’re ever submitting code to the forums please use the formatting options which avoids this whole issue- How do I format code in my posts? though I’d suggest keeping the copy/paste to a minimum anyway :wink: and instead grasping the idea from any helpful guidance but implementing it yourself :slightly_smiling_face:.

Thank you tgrtim ji. I second that.

pizza_and_prices.insert(2,“(2.5, ‘peppers’)”)
print(pizza_and_prices)