insert() in Python

Video Tutorial
FREE
List Operations thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

Overview

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.

Now the index will be used to insert the new element into the list.

In the below example, let's insert element 4 before element 5.

Code:

Output:

We can see that element 4 is inserted before element 5.

Conclusion

  • insert() Function is a Python library function that is used to insert the given element at a particular index in a list.
  • Syntax of the insert() function is, My_list.insert(index, element)
  • insert() function takes 2 parameters, index and element.
  • There is no return value in insert() function in Python.
  • This funciton will throw AttributeError if anything other than the list is used to insert an element.
  • We can insert elements using many approaches as discussed above.

See Also