Python List insert() Method (with Examples)

Python List insert() Method: In this tutorial, we will learn about the insert() method of the list class with its usage, syntax, parameters, return type, and examples. By IncludeHelp Last updated : June 20, 2023

Python List insert() Method

The insert() is an inbuilt method of the list class that is used to add an element /item at a specified index to the list. The insert() can add the element where we want i.e. position in the list, which we were not able to do the same in the append() Method.

Syntax

The following is the syntax of insert() method:

list.insert(index, element)

Parameter(s):

The following are the parameter(s):

  • list is the name of the list.
  • index is the valid value of the position/index.
  • element is an item/element to be inserted.

Return Value

Method insert() does not return any value, it just modifies the existing list.

Example 1: Use of List insert() Method

# Python program to demonstrate
# an example of list.insert() method

# list
cities = ["New Delhi", "Mumbai"]

# print the list
print("cities are: ", cities)

# insert 'Bangalore' at 0th index
index = 0
cities.insert(index, "Bangalore")

# insert 'Chennai' at 2nd index
index = 2
cities.insert(index, "Chennai")

# print the updated list
print("cities are: ", cities)

Output

cities are:  ['New Delhi', 'Mumbai']
cities are:  ['Bangalore', 'New Delhi', 'Chennai', 'Mumbai']

Explanation

  • Initially there were two elements in the list ['New Delhi', 'Mumbai'].
  • Then, we added two more city names (two more elements) - 1) 'Bangalore' at 0th index and 2) 'Chennai' at 2nd index after adding 'Bangalore' at 0th index, by using following python statements:
  • # insert 'Bangalore' at 0th index
    index = 0
    cities.insert(index, 'Bangalore')
    
    # insert 'Chennai' at 2nd index
    index = 2 
    cities.insert(index, 'Chennai')
    
  • After inserting 'Bangalore' at 0th index, the list will be ['Bangalore', 'New Delhi', 'Chennai'].
  • And, then we added 'Chennai' at 2nd index. After inserting 'Chennai' at 2nd index. The list will be ['Bangalore', New Delhi', 'Chennai', 'Mumbai'].

So, it is at our hand, what we have to do? If our requirement is to add an element at the end of the list - use list.append() Method, or if our requirement is to add an element at any index (any specified position) - use list.insert() Method.

Example 2: Use of List insert() Method

Insert a list to an existing list at the specified index.

# list
cities = ["New Delhi", "Mumbai"]

# list to be added
newcities = ["Bangalore", "Chennai"]

# print the list
print("cities are: ", cities)

# insert list at 0th position
index = 0
cities.insert(index, newcities)

# print the updated list
print("Updated cities are: ", cities)

Output

cities are:  ['New Delhi', 'Mumbai']
Updated cities are:  [['Bangalore', 'Chennai'], 'New Delhi', 'Mumbai']

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.