insert() in Python
![]()
insert() function is a Python library function used to insert a given element at a given index in a list. After the insertion of a new element in the list, elements after the inserted element are shifted by one index to the right side.
Syntax of insert() function in Python
insert() function simply takes two parameters index at which the element is to be inserted and the element which is to be inserted.
Parameters of insert() function in Python
Below are the parameters of the insert function:
- index: The index at which the element is inserted. This parameter is of number type.
- element: It is the element that is to be inserted into the list. This parameter can be of any data type(string, number, object).
Return Value of insert() function in Python
insert() method just inserts the element in the list and updates the list and so does not return any value or we can say that insert() method returns None .
Example of insert() function in Python
Let's take a look at a basic example of how insert() function is used:
Output: We have simply added the string in the list:
What is the use of insert() function in Python?
Suppose you have been given some work by your teacher where you are arranging marks of students in descending order. Currently, you have stored [99, 90, 80, 70] in the list and want to store 95 in the list, so one way is that you can append 95 to the list and then sort it, But it would take O(n logn) time complexity for just one element because for sorting we will be applying sort() function to make the code easy, but sort() function uses merge sort which has O(n logn) time complexity.
So why would you make your code complex and costly when you can use the inbuilt function which will store the given element at the given index in your list. Inserting an element using insert() function takes O(n) time complexity.
So taking the above example, using the insert function you will just insert the element "95" at the 1st index and your problem will be solved and the final list will be [99, 95, 90, 80, 70] .
As the name suggests, the insert() function in python is used to insert the given element at a particular index in a list.
Errors in insert() function
insert() method will throw AttributeError , if anything other than list is used to insert an element.
Example
If we will try to insert an element in the string we will get an error because the string value does not have any insert() function.
Output:
An error will occur saying 'str' object has no attribute 'insert'.
Examples of insert() function in Python:
Now we have learned about the insert function and now we can apply the insert() function in some of our examples.
Example 1: Inserting an element at the beginning of the list
Let's take a simple example where we have to insert an element at the beginning of the list.
We have simply passed the given element with index parameter as 0 .
Code:
Output:
Simply element 11 is inserted at the 0th index and all elements after 11 are shifted one index to the right.
Example 2: Inserting an Element at the end of the list
Let's take a simple example where we have to insert an element at the end of the list.
We have simply passed the given element with the index parameter as len(MyList). So insert() method will insert the element at the end of the list.
Code:
Output:
Simply element 10 is inserted at last, just like the append function.
Example 3: Inserting a string value into a list
We will pass a string value as the given element with index parameter 2, So the insert() method will insert the string value at the 3rd position of our list.
Code:
Output:
The element 'c' is inserted at the 3rd position.
Example 4: Using negative indexes to insert into the list
We will use negative indexes like -1 and -2 as the index parameter which will insert the elements to the 2nd last and 3rd last position of the list.
Code:
Output:
Element 10 and 11 are inserted at 2nd and 3rd last positions respectively.
Example 5: Inserting a tuple in a list
We will make a tuple with some values and will insert the tuple at the beginning of our list.
Code
Output:
We can see in the output that the tuple (1, 2, 3) is inserted at the beginning of the list.
Example 6: Insert before any element
Let's take a case where we have to insert a given element just before any element, so for that, we will just get the index of the element before which the element is to be inserted using the index() function.
Python List insert() – How to Add to a List in Python

Ihechikara Vincent Abba

The list data type is one of the built-in data structures in Python along with sets, tuples, and dictionaries. You use a list to organize, group, and store data.
But each of these data structures has distinctive features that differentiates them from each other.
In this article, we’ll see how to create a list in Python. We’ll also see how to add items to a list using the insert() , append() , and extend() methods.
How to Create a List in Python
To create a list in Python, we use square brackets. Here’s an example:
In the code above, we created a list called myList with three items – «one», «two», and «three». As you can see above, the items are in the square brackets.
How Do You Add an Item to a List in Python?
There are three methods we can use when adding an item to a list in Python. They are: insert() , append() , and extend() . We’ll break them down into separate sub-sections.
How to Add an Item to a List Using the insert() Method
You can use the insert() method to insert an item to a list at a specified index. Each item in a list has an index. The first item has an index of zero (0), the second has an index of one (1), and so on.
Here’s an example using the insert() method:
In the example above, we created a list with three items: [‘one’, ‘two’, ‘three’] .
We then used the insert() method to insert a new item – «zero» at index 0 (the first index): myList.insert(0, ‘zero’) .
The insert() method takes in two parameters – the index of the new item to be inserted and the value of the item.
How to Add an Item to a List Using the append() Method
Unlike the insert() method which allows us to specify where to insert an item, the append() method adds the item to the end of the list. The new item’s value is passed in as a parameter in the append() method.
Here is an example:
The new item was passed in as a parameter in the code above: myList.append(‘four’) .
When printed to the console, we have the item at the last index of the list.
How to Add an Item to a List Using the extend() Method
You can use the extend() method to append a data collection to a list. I’m using «data collection» here because we can also append sets, tuples, and so on to a list.
Let’s see some examples.
How to Append a List to a List Using the extend() Method
In the code above, we created two lists – myList1 and myList2 . Next, we’ll append the items in the second list to the first one.
So when we print myList1 , we’ll have this: [‘one’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’] .
Here’s everything put together:
How to Append a Tuple to a List Using the extend() Method
The process here is the same as the last example, except that we’re appending a tuple. You create a tuple using parentheses.
Let’s append a tuple to a list using the extend() method.
We get the same result as in the last section.
Conclusion
In this article, we talked about lists in Python.
We saw how to create a list and the various methods for adding items to a list.
We added items to our list using the insert() , append() , and extend() methods.
The insert() method inserts a new item in a specified index, the append() method adds a new at the last index of a list, while the extend() method appends a new data collection to a list.
How To add Elements to a List in Python

In this tutorial, we will learn different ways to add elements to a list in Python.
There are four methods to add elements to a List in Python.
- append() : append the element to the end of the list.
- insert() : inserts the element before the given index.
- extend() : extends the list by appending elements from the iterable.
- List Concatenation: We can use the + operator to concatenate multiple lists and create a new list.
Prerequisites
In order to complete this tutorial, you will need:
- Familiarity with installing Python 3. And familiarity with coding in Python. How to Code in Python 3 series or using VS Code for Python.
This tutorial was tested with Python 3.9.6.
append()
This function adds a single element to the end of the list.
This example added Orange to the end of the list.
insert()
This function adds an element at the given index of the list.
This example added 20 at the index of 2 . 20 has been inserted into the list at this index.
extend()
This function adds iterable elements to the list.
This example added a list of [1, 2] . Then it added a tuple of (3, 4) . And then it added a string of ABC .
List Concatenation
If you have to concatenate multiple lists, you can use the + operator. This will create a new list, and the original lists will remain unchanged.
This example added the list of evens to the end of the list of odds . The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.
Conclusion
Python provides multiple ways to add elements to a list. We can append an element at the end of the list, and insert an element at the given index. We can also add a list to another list. If you want to concatenate multiple lists, then use the overloaded + operator
References:
Want to deploy your application quickly? Try Cloudways, the #1 managed hosting provider for small-to-medium businesses, agencies, and developers — for free. DigitalOcean and Cloudways together will give you a reliable, scalable, and hassle-free managed hosting experience with anytime support that makes all your hosting worries a thing of the past. Start with $100 in free credits!
Python List insert()
In this tutorial, we will learn about the Python List insert() method with the help of examples.
The insert() method inserts an element to the list at the specified index.
Example
Syntax of List insert()
The syntax of the insert() method is
Here, elem is inserted to the list at the i th index. All the elements after elem are shifted to the right.
insert() Parameters
The insert() method takes two parameters:
- index — the index where the element needs to be inserted
- element — this is the element to be inserted in the list
Notes:
- If index is 0, the element is inserted at the beginning of the list.
- If index is 3, the index of the inserted element will be 3 (4th element in the list).
Return Value from insert()
The insert() method doesn’t return anything; returns None . It only updates the current list.