Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
The insert() Method | List
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

book
The insert() Method

What if we want to add an item not at the end of the list, but at a specific position? For this purpose, we use the insert() method.

The syntax for the insert() method is:

  • index: the position in the list where you want to add the new element. Remember, Python uses zero-based indexing;
  • element: the item you want to insert into the list.

Imagine planning a trip and having a list of cities you want to visit.

123
travel_wishlist = ["Paris", "Oslo", "Kyoto", "Sydney"] print(travel_wishlist) # Output: ['Paris', 'Oslo', 'Kyoto', 'Sydney']
copy

This list contains four items:

However, your plans change, and you decide to adjust the order of destinations. Now, you want to prioritize "Chicago" as the first destination.

12345
travel_wishlist = ["Paris", "Oslo", "Kyoto", "Sydney"] # Adding "Rome" as the first destination travel_wishlist.insert(0, "Chicago") print(travel_wishlist) # Output: ['Chicago', 'Paris', 'Oslo', 'Kyoto', 'Sydney']
copy

After this, "Chicago" takes the 0 index. It's now at the top, and the rest of the items have shifted down. So, we now have 5 items:

Note

With the insert() function, you can add only one item at once.

Task
test

Swipe to show code editor

You have the original travel wishlist: ["Paris", "Oslo", "Kyoto", "Sydney"].

You've decided to prioritize two specific cities for your travels using the insert() method.

  • Add the first city as the new first destination in your list;
  • Then, add the second city right after the trip to Paris.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7
toggle bottom row

book
The insert() Method

What if we want to add an item not at the end of the list, but at a specific position? For this purpose, we use the insert() method.

The syntax for the insert() method is:

  • index: the position in the list where you want to add the new element. Remember, Python uses zero-based indexing;
  • element: the item you want to insert into the list.

Imagine planning a trip and having a list of cities you want to visit.

123
travel_wishlist = ["Paris", "Oslo", "Kyoto", "Sydney"] print(travel_wishlist) # Output: ['Paris', 'Oslo', 'Kyoto', 'Sydney']
copy

This list contains four items:

However, your plans change, and you decide to adjust the order of destinations. Now, you want to prioritize "Chicago" as the first destination.

12345
travel_wishlist = ["Paris", "Oslo", "Kyoto", "Sydney"] # Adding "Rome" as the first destination travel_wishlist.insert(0, "Chicago") print(travel_wishlist) # Output: ['Chicago', 'Paris', 'Oslo', 'Kyoto', 'Sydney']
copy

After this, "Chicago" takes the 0 index. It's now at the top, and the rest of the items have shifted down. So, we now have 5 items:

Note

With the insert() function, you can add only one item at once.

Task
test

Swipe to show code editor

You have the original travel wishlist: ["Paris", "Oslo", "Kyoto", "Sydney"].

You've decided to prioritize two specific cities for your travels using the insert() method.

  • Add the first city as the new first destination in your list;
  • Then, add the second city right after the trip to Paris.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt