FAQ: Working with Lists in Python - Adding by Index: Insert

This community-built FAQ covers the “Adding by Index: Insert” exercise from the lesson “Working with Lists in Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Scientist
Analyze Data with Python
Computer Science
Analyze Financial Data with Python
Build Chatbots with Python
Build Python Web Apps with Flask
Data Analyst

Learn Python 3
CS101 Livestream Series

FAQs on the exercise Adding by Index: Insert

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

#Which of the following lines of code will insert “Gus” as the middle element of the list friends?

friends = [“Annabelle”, “Greg”, “Katya”, “Sol”]

1, insert(friends, 2, “Gus”)

2, friends.insert(-2, “Gus”)

3, friends.insert(1, “Gus”)

4, friends.insert(-3, “Gus”)

Why the answer is not 4?

2, friends.insert(-2, “Gus”)
print(friends)
[“Annabelle”, “Greg”, “Gus”, “Katya”, “Sol”]

4, friends.insert(-3, “Gus”)
print(friends)
[“Annabelle”, “Gus”, “Greg”, “Katya”, “Sol”]

If you’re posting code to the forums please see- How do I format code in my posts? as it makes it much easier for everyone else to read.

As you can see from your printed output, "Gus" is not the middle element of the resulting list when using an index of -3. Since you included the second example too is that the one you meant?

For an item with 4 elements the indices listed below are equivalent when using lst[index]-

->  0,  1,  2,  3
-> -1, -2, -3, -4

So friends.insert(2, "Gus") or friends.insert(-2, "Gus") would create equivalent objects in this example-

friends = ["Annabelle", "Greg", "Katya", "Sol"]
lst1 = friends.insert(2, "Gus")
friends = ["Annabelle", "Greg", "Katya", "Sol"]
lst2 = friends.insert(-2, "Gus")
print(lst1 == lst2)
Out: True
1 Like

Hi tgrtim,
Thank you for replying! I am a new learner with 0 experience with anything related to computer program. :joy: I will take a look of the format.

In this question, I thought the correct answer is friends.insert(-3, “Gus”).

In the lecture Accessing List Elements: Negative Index
I learned that
pancake_recipe = [“eggs”, “flour”, “butter”, “milk”, “sugar”, “love”]

Element Index
eggs -6
flower -5
butter -4
milk -3
sugar -2
love -1

Following this example, “Sol” is -1, “Katay” is -2, I want to add a new name “Gus” before “Katya”, should I add it to -3? I am still confused.

Thank you again for taking time read my post and replying!

1 Like

Whoops, sorry. Wrote those indices backwards, it should’ve been-

->  0,  1,  2,  3
-> -4, -3, -2, -1

and… I messed up the code afterwards, it’s not my day. Sorry about that :dizzy_face:

friends1 = ["Annabelle", "Greg", "Katya", "Sol"]
friends1.insert(2, "Gus")
friends2 = ["Annabelle", "Greg", "Katya", "Sol"]
friends2.insert(-2, "Gus")
print(friends1, "\n", friends2)
print(friends1 == friends2)

Now it’s a little more obvious about 2 and -2 lining up to provide an index to the same element for the example with 4 elements.

The docs specify how insert works, see- 5. Data Structures — Python 3.9.5 documentation It insert the element at that exact index (so you’d move the current element and further elements one to the right).

As you said, “Katay” is the element found at friends[-2] (we could also address the same element with friends[2] in this case). If we insert “Gus” at this exact it moves the current element and any elements to the right one step further to the right.

                               friends[2]
                                   |
friends = ["Annabelle", "Greg", "Katya", "Sol"]
friends.insert("Gus", 2)

                            "Gus" goes here
                                   |
friends = ["Annabelle", "Greg",         ,  "Katya", "Sol"]
                                              |
                          The previous element at that location,
                        and the elements to the right, shift right

When you’re trying to get your head around a concept like this I think actually testing it and seeing the result helps no end. If you trial the code I added above (with .insert and print) it should show this but feel free to play around with it and keep printing the outcome so you know for sure. There’s no need for programming to be a black box :slightly_smiling_face:.

1 Like

Thank you so much! I have tired the code you provided. The test shows friends1.insert(2, “Gus”) and friends2.insert(-2, “Gus”) have same result.

The picture showing how insert( ) moves the previous element and the element to the right shift one space to the right helps a lot! It gives me a clear thinking path. Now I know why friends.insert(-3, “Gus”) output [“Annabelle”, “Gus”, “Greg”, “Katya”, “Sol”]. Since I want to add “Gus” to the position “Greg” stands, this action moves “Greg” and any elements to the right of “Gerg” shift one space to the right. “Gus” is ends up at [-4].

Thank your for you time! And hope you have a wonderful day! :smiley:

1 Like

Hi I am really new to this just 10% into my computer science course have a small question about
below insert code; when I inserted the Pineapple it would not print until I moved the print code to below the insert code is this how it works?
front_display_list = [“Mango”, “Filet Mignon”, “Chocolate Milk”]

Your code below:

front_display_list.insert(0, “Pineapple”)
print(front_display_list)

If I understand you correctly then yes that’s exactly how it works. Your commands are run in order. If you used print before the .insert() then you’d be printing the list as it is at that moment. Try printing before and after if you wanted to see the difference.

1 Like

I understand the point of .index(), though I think I’m missing something. A couple of exercises ahead of here is a project where you need to help a pizza place work out a bunch of stuff, and one point that I had trouble is a hint told me to use the .index() to add into the 2D list, the element [2.5, peppers]. If I attempt to type:

pizza_and_prices = pizza_and_prices = [[2.00, "pepperoni"], [6.00, "pineapple"], [1.00, "cheese"], [3.00, "sausage"], [2.00, "olives"], [7.00, "anchovies"], [2.00, "mushrooms"]] pizza_and_prices.insert(2.5, "peppers") print(pizza_and_prices)

into the code, it throws me a typeerror of “Integer Argument Expected, Got Float”

Are floats not allowed to be used in an .insert(),?

Also, this is editted down, as I’m brand new to this and I’m sure the full page is a mess, however the coding above throws an identical error, so I assume the answer can be found with it.

Yes your code snippet makes sense, ta. I think you meant .insert instead of .index here but apologies if that’s not the case. The .insert() method of a list object takes two arguments, an object to insert and the index at which to insert it- https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types

So the issue you have in your example is that you are supplying a supposed index of 2.5 and lists only support integer indicies. If you want to insert [2.5, "peppers"] as a single element you’d need to pass the correct index and pass that entire object. Here’s a closer example of how that might look-

[].insert(0, ["a", "b"])

I think there might be a dev comment on this particular project you mention, it may be worth reading to see if it is relevant-

1 Like