Post

Python List insert()

In this tutorial, we will learn about the python insert() method.

Python List insert()

Syntax of python list insert():

1
2
list.insert(index, element)

insert() Parameters:

The inser() methods takes two parameters as arguments.

  • index - the index or position where the elements needs to be inserted.
  • element - the element to be inserted in the list(can be integer, string , list or any python data types).

Lets check an example of insert() method.

1
2
3
4
5
6
7
8
9
10
Example 1: How to insert element in the list?

nums = [5,2,4,6,1,9]

print("List before insert() method: ",nums)

nums.insert(5,10)

print("List after insert() method: ",nums)

The output will be as follow:

1
2
List before insert() method:  [5, 2, 4, 6, 1, 9]
List after insert() method:  [5, 2, 4, 6, 1, 10, 9]

Rules of list insert() method

  • In insert() method it is compersuly to give index of the list to add the element.
  • insert() method will not return anything.
  • Python list index starts from 0 so insert() method will work accordingly.
This post is licensed under CC BY 4.0 by the author.