In this Python tutorial, we will discuss the Python List insert() method. We will explore the insert() method in Python for adding elements to a list with examples inspired by real-life scenarios.
List insert() method in Python
Below are the topics that we are doing to discuss in this article:
- Introduction to Python List insert() method
- Syntax of the insert() method
- Examples of using insert() method in real-world applications
- Using negative indices with insert() method
insert() method in Python
The insert()
method is used to insert an element at a specified index in a Python list. This method does not replace any element in the Python list. Instead, it shifts the elements to the right of the specified index to make room for the new element.
The syntax of the Python insert()
method is as follows:
list.insert(index, element)
index
: The position where the element should be inserted. This is a required parameter.element
: The item to be inserted in the list. This is a required parameter.
The return value of insert()
method:
The insert()
method doesn’t return anything. It just modifies the original Python list object by inserting the new element at the specified position.
insert() method in Python List Examples
Below are some examples demonstrating how to use the insert()
method in various scenarios.
Example:1
Imagine having a Python list of popular U.S. states that we want to update by inserting a new state at a specific position.
trending_states = ["California", "Texas", "Florida", "New York"]
trending_states.insert(2, "Washington")
print(trending_states)
In this example, we have a Python list of popular U.S. states, and we use the insert()
method to add a new state (“Washington”) at a specific position (index 2). After the operation, the order of the states in the Python list is updated, with “Washington” appearing between “Texas” and “Florida”.
Output:
Example:2
Suppose we manage an online store’s inventory and need to add a new product at a particular index.
inventory = ["iPhone", "Samsung Galaxy", "Google Pixel", "OnePlus"]
inventory.insert(1, "Motorola Edge")
print(inventory)
In this scenario, we manage an online store’s inventory and need to insert a new product (“Motorola Edge”) at a specific index (1) in the Python list. After the insert()
method is applied, “Motorola Edge” appears between “iPhone” and “Samsung Galaxy” in the inventory list.
Output:
Example:3
Here, we have a list of characters, and we want to insert a new list of characters.
us_states = ['Alabama', 'Alaska', 'Arizona']
us_states.insert(3, ['Colorado', 'Arkansas', 'California'])
print(us_states)
In this example, we have a list of US states named us_states
, which initially contains the states ‘Alabama’, ‘Alaska’, and ‘Arizona’. We then use the Python insert()
method to insert a new list of states ['Colorado', 'Arkansas', 'California']
at index 3 of the original list.
The output of the print()
function shows the updated Python list, which includes the new list of states as a single element at index 3. Therefore, the resulting list has four elements: ‘Alabama’, ‘Alaska’, ‘Arizona’, and ['Colorado', 'Arkansas', 'California']
.
Output:
Example:4
Picture managing a basketball team roster and having to add a new player at a specific position.
all_star_team = ["LeBron James", "Stephen Curry", "Kevin Durant", "James Harden"]
all_star_team.insert(-3, "Russell Westbrook")
print(all_star_team)
In this example, we have a Python list of basketball players named all_star_team
that includes LeBron James, Stephen Curry, Kevin Durant, and James Harden. We then use the insert()
method to insert the player Russell Westbrook at the second-to-last position of the Python list, using an index value of -3
.
The output of the print()
function shows the updated list, which includes the new player Russell Westbrook in the second-to-last position.
Output:
Note: Using a negative index value with insert()
is a useful technique for inserting elements at a specific position relative to the end of the Python list, instead of counting from the beginning of the list.
Conclusion
In this article, we’ve learned about the Python List insert() method and how to use them with various examples. The insert() method allows us to add elements to a list at a specific position, making it a powerful tool for working with dynamic data structures.
You may also like to read the following articles:
- Python List extend() method [With Examples]
- Python Program to convert String to a List
- Python merge two lists without duplicates
- 7 ways to concatenate two lists in Python
- Python Program to Convert Two Lists Into a Dictionary
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.