This is a detailed tutorial of the Python List insert() method. Learn to insert an item into the list at a particular index using the insert() method.
Table of Contents
Python List insert()
The list.insert()
method is used to insert an item at a particular index into a given Python List. The new item to be inserted and the index at which it is to be inserted has to be provided as the second and first arguments to this method respectively.
Syntax
The following syntax is to insert the item
at the index
in the list
.
1 |
list.insert(index, item) |
insert()
Parameters
As shown in the syntax, the method insert()
takes two arguments. These are described below.
- index. This is the numerical index that represents the position where the item is to be inserted. The first item of any list is at index 0, the second item at index 1 and so on it continues.
- item. This is the actual item that is to be inserted. It could be any Python Object or may belong to any Data Type.
insert()
Return Value
As the list.insert()
method simply inserts a new item into the list, so it does not return anything i.e. it returns None
.
Examples
The usage of the Python List insert() method is quite easy and is demonstrated below in the form of example code snippets.
Example 1. Inserting a new item (element) to a Python List
Here I’ve defined a list with name students
. Then I’m adding a new item "Manik"
at index 4
i.e. at the 5th
position in the list as the indexing of the list starts from 0
and not from 1
.
1 2 3 4 5 6 7 8 9 10 11 12 |
#Example 1 - Python List insert() #A List of students students = ["Gurmeet","Preety","Deepinder","Jaskaran","Daman"] print("Initial List Items:",students) #Adding a new Item #Manik at index 4 students.insert(4, "Manik") print("Updated List Items:",students) |
As you can see in the following screenshot of the output that the list is updated with the new item "Manik"
inserted at index 4 while the other items after "Manik"
will shift their index forward by 1
.
Example 2. Inserting new Collection Objects (Tuple, Set, Dictionary) into a List
As I mentioned earlier in this article that you can insert a new item of any data type into an existing Python List. It does not matter at all that the list items should be of the same data type. Therefore, in the following example, I’m creating a list containing items of multiple collections-based data types.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#Example 2 - Python List insert() #A List with different collection objects collections = [(1,2,3),[4,5,6],{7,8,9}] print("Initial List:",collections) #Inserting a new List Object at index 0 collections.insert(0,['A','B','C']) print("Updated List:",collections) #Inserting a new Set Object at index 2 collections.insert(2,{'D','E','F'}) print("Updated List:",collections) #Inserting a new Tuple Object at index 4 collections.insert(4,('G','H','I')) print("Updated List:",collections) |
Here, I’ve first created a list named collections
with different collection objects like a Tuple, a List, and a Set as the list items. Then I’m inserting another List, another Tuple, and another Set as individual list items to the collections
List using the insert() method at particular indexes.
The above method is to insert new collection objects like another List, Tuple or Set as items of the list. If you want to merge the items of two different collection objects into one collection object, you can use the Python List extend()
method or simply the +
operator. The following linked articles may help in that case.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!