7

Python List insert()

 1 year ago
source link: https://thispointer.com/python-list-insert/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Advertisements

In this article, we will learn about the insert() method of List in Python.

Table Of Contents

Introduction

List provides a function insert(index, elem) to insert an element at a specified index position in the List. It means we can insert element in between the existing elements of list. During insertion, all the elemnts from this index position onwards will be shifted by 1 in the right.

Syntax of insert() method of List

list.insert(index, element)
list.insert(index, element)

Parameters of insert() method

It accepts two arguments,

  • index : The index position at which element needs to be inserted. Index starts from 0 in the list. So, the first element has index 0, second element has index 1, and ith element has index position i-1.
  • element: An object that needs to be inserted at given index position in the list. It can be an integer, string, boolean, tuple, list or any other object.

Return value of insert() method

It inserts the given element at the specified index position in the list. It modifies the list in place, therefore it returns None, basically it returns nothing.

Advertisements

Examples of List insert()

Insert a string at a specific position in the List of strings

Suppose we have a list of five strings, and we want to insert a new string as the third element of list. As indexing starts from 0, therefore the index of 3rd element is 0. So, we will call the insert() function of list, and pass index position 2, and a new string as arguments in it. It will insert this new string at index 2 in the list. Let’s see the complete example,

listOfNames = ['John', 'Mark', 'Ritika', 'Atharv']
# Insert a string at index 2 in the list
listOfNames.insert(2, 'Smriti')
print(listOfNames)
listOfNames = ['John', 'Mark', 'Ritika', 'Atharv']

# Insert a string at index 2 in the list
listOfNames.insert(2, 'Smriti')

print(listOfNames)

Output:

['John', 'Mark', 'Smriti', 'Ritika', 'Atharv']
['John', 'Mark', 'Smriti', 'Ritika', 'Atharv']

Insert an integer at a specific position in the List

Suppose we have a list of five strings, and we want to insert a new string as the third element of list. As indexing starts from 0, therefore the index of 3rd element is 0. So, we will call the insert() function of list, and pass index position 2, and a new string as arguments in it. It will insert this new string at index 2 in the list. Let’s see the complete example,

listOfNumbers = [11, 12, 13, 14, 15]
# Insert a number at index 2 in the list
listOfNumbers.insert(2, 99)
print(listOfNumbers)
listOfNumbers = [11, 12, 13, 14, 15]

# Insert a number at index 2 in the list
listOfNumbers.insert(2, 99)

print(listOfNumbers)

Output:

[11, 12, 99, 13, 14, 15]
[11, 12, 99, 13, 14, 15]

What if we pass wrong index position in insert()?

If you pass an index in the insert() function, which is greater than the number of elements in list, then it will add that element at the end of List. Let’s see the example,

listOfNames = ['John', 'Mark', 'Ritika', 'Atharv']
# Insert a string at index 20 in the list
listOfNames.insert(20, 'Smriti')
print(listOfNames)
listOfNames = ['John', 'Mark', 'Ritika', 'Atharv']

# Insert a string at index 20 in the list
listOfNames.insert(20, 'Smriti')

print(listOfNames)

Output:

['John', 'Mark', 'Ritika', 'Atharv', 'Smriti']
['John', 'Mark', 'Ritika', 'Atharv', 'Smriti']

As we passed the index 20 in the insert() function, but list had only 5 elements, therefore it added the element at the end of list.

Summary

We learned all about the insert() method of list in Python. Thanks.

Advertisements

Thanks for reading.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK