Python 3 - List insert() Method


Advertisements


Description

The insert() method inserts object obj into list at offset index.

Syntax

Following is the syntax for insert() method −

list.insert(index, obj)

Parameters

  • index − This is the Index where the object obj need to be inserted.

  • obj − This is the Object to be inserted into the given list.

Return Value

This method does not return any value but it inserts the given element at the given index.

Example

The following example shows the usage of insert() method.

Live Demo
#!/usr/bin/python3

list1 = ['physics', 'chemistry', 'maths']
list1.insert(1, 'Biology')
print ('Final list : ', list1)

Result

When we run above program, it produces the following result −

Final list :  ['physics', 'Biology', 'chemistry', 'maths']

python_lists.htm

Advertisements