Python List insert()

Python List insert() is a built-in function that inserts the given element at a specified index. 

In this tutorial, we will learn about the Python list insert() method with the help of examples.

Syntax of List insert() 

The syntax of the insert() method is:

list.insert(index, element)

insert() Parameters

The insert() method takes two parameters.

  • index – The index or position where the elements need to be inserted.
  • element – The element or value (string, number, object etc.) to be inserted in the list. 

Return Value from List insert()

The insert() method modifies the list by inserting an element at the specified position, but it does not return any value.

Notes:

  • If the index is specified as 0, the element is inserted at the beginning of the list
  • If the index>=length(list), the element is inserted at the end of the list

Example 1: Inserting an Element to the List

# list of vowels
vowels = ['a','e','i','o']

# insert a new vowel into a list
vowels.insert(4,'u')

# print after inserting new vowel
print("List After Inserting = ",vowels)

Output

List After Inserting =  ['a', 'e', 'i', 'o', 'u']

Example 2: Inserting an element at the start and end of the list

To insert an element at the start of the list, you can give index as 0. For inserting at the end of the list, we can give the length of the list.

Even if we give index which is out of range, the element will get inserted at the end of the list and Python will not raise any exception.

# list of vowels
vowels = ['e','i','o']

# insert a new vowel into a list
vowels.insert(0,'a')

# print after inserting at the start of the list
print("Insert at the beginning of the list = ",vowels)

# insert a new vowel at the end a list
vowels.insert(30,'u')

# print after inserting new vowel at the end
print("Insert at the end of the list = ",vowels)

Output

Insert at the beginning of the list =  ['a', 'e', 'i', 'o']
Insert at the end of the list =  ['a', 'e', 'i', 'o', 'u']

Example 3: Inserting a tuple (as an element) to the list

# list of vowels
vowels = ['a','e','i']

tuple_vow = ('o','u')

# insert tuple after index 3
vowels.insert(3,tuple_vow)

# print the list with tuple
print("List with tuple = ",vowels)

Output

List with tuple =  ['a', 'e', 'i', ('o', 'u')]
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Split List Into Chunks

Python Split list into chunks

Table of Contents Hide Python Split list into chunksMethod 1: Using a For-LoopMethod 2: Using the List Comprehension MethodMethod 3: Using the itertools MethodMethod 4: Using the NumPy MethodMethod 5:…
View Post
Python List Index()

Python List index()

Table of Contents Hide Syntax of List index() index() ParametersReturn Value from List index()Example 1: Find the index of the elementExample 2: Index of the Element not Present in the List…
View Post
Python String Rstrip()

Python String rstrip()

Table of Contents Hide rstrip() Syntaxrstrip() Parametersrstrip() Return Value Example 1: Working of rstrip() Example 2 – How to use rstrip() method in real world? The Python String rstrip() method…
View Post
Python Trim String Strip() Vs Rstrip() Vs Lstrip()

Python Trim String – rstrip(), lstrip(), strip()

Table of Contents Hide Difference between strip() vs rstrip() vs lstrip()strip()Example of strip() function  rstrip() Example of rstrip() function   lstrip() Example of lstrip() function   Python provides three methods to trim the…
View Post