Add item to a specific position in list Python Programming
Hello learners, in this Python tutorial we are going to learn how we can add an item to a specific position in a list in Python.
In my previous tutorial, I have shown you How to add items to a Python list. Then I thought if someone needs to insert items to a specific position of a list in Python. Thus I came here again with a new tutorial to teach you how to add elements to a specific index position in a list.
Insert Item to a specific position in a Python list
You will be glad to know that Python makes it easier for us with a predefined method which is the
Below is given the syntax for the insert():
This method has two parameters.
- Index – This is the position where the new element is going to be inserted.
- Element – This is the new element that you are going to insert into the existing list.
I will provide some examples so that it can be more understandable.
Suppose, you have a list like this below:
Now you have to add an element to this list. But the element should be added to a particular position. You need to add the new element between 36 and 98. That means the position of the new element has to be index no 2.
my_list =[85,36,98,54] my_list.insert(2,'this is inserted') print(my_list)
[85, 36, 'this is inserted', 98, 54]
You can use both integer or string values in the second parameter of
Note: This method does not return any value.