Question on lesson "Adding by index: Insert"

Hello everyone!
I’m currently on: Learn Python 3 > Lists > Working with Lists in Python > Adding by Index: insert
Learn Python 3 | Codecademy

So here is my question:
I’m doing this exercise and decided to start adding a couple of elements using different indexes. For “Strawberries”, I tried to insert it in index -1, which to my understanding is supposed to insert the element to the end of my list, but it’s inserted in front of “Chocolate Milk”.

front_display_list = [“Mango”, “Filet Mignon”, “Chocolate Milk”]
front_display_list.insert(0, “Pineapple”)
front_display_list.insert(-1, “Strawberries”)

Outputs:

[‘Pineapple’, ‘Mango’, ‘Filet Mignon’, ‘Strawberries’, ‘Chocolate Milk’]

My best guess is that if I wanted to put it at the end of the list, I would use “append” instead of insert?

1 Like

Your thought process is solid, but the insert function places the object before the index indicated. Since 'Chocolate Milk' is in index -1 the inserted 'Strawberries' is now before that object in the list.

As you suspected you can use .append(item) to place it at the end of the list. If you wanted to use insert you can use .insert(len(x), item) to place a new object at the end of the list.

1 Like

Thank you so much! I super appreciate it :slightly_smiling_face: