Javatpoint Logo
Javatpoint Logo

Python List insert(i,x) Method

Python insert() method inserts the element at the specified index in the list. The first argument is the index of the element before which to insert the element.

Signature

Parameters

i : index at which element would be inserted.

x : element to be inserted.

Return

It does not return any value rather modifies the list.

Let's see some examples of insert() method to understand it's functionality.

Python List insert() Method Example 1

Let's see an example to insert an element at 3 index of the list.

Output:

1
2
3
After extending:
1
2
3
4

Python List insert() Method Example : list

It is possible to insert a list as an element to the list. See the example where a list is inserted at specified index.

Output:

1
2
3
After inserting:
1
2
3
['4', '5', '6']

Python List insert() Method Example : Tuple

It is possible to insert a tuple as an element to the list. See the example where a tuple is inserted at specified index.

Output:

1
2
3
After inserting:
1
2
3
('4', '5', '6')

Python List insert() Method Example : Set

It is possible to insert a set as an element to the list. See the example where a set is inserted at specified index.

Output:

1
2
3
After inserting:
1
2
3
{'4', '5', '6'}

Next TopicPython Lists




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA