Python List insert()
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.
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)
Parameters of insert() method
It accepts two arguments,
index
: The index position at which element needs to be inserted. Index starts from0
in the list. So, the first element has index 0, second element has index 1, andith element
has index positioni-1
.element
: An object that needs to be inserted at givenindex
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)
Output:
['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)
Output:
[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)
Output:
['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
-
12
Prerequisites To learn about a singly linked list, you should know: Python 3 OOP concepts What are singly linked lists? In this tutorial, we will learn about what singly linked lists are and...
-
2
MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in...
-
5
Linked List - Insert a node at the end of the list in C advertisements This is the code for insertion of a node at the end. I am not getting a...
-
3
C #: Reflection to insert elements into a generic list advertisements I need to add an object created by reflection into a generic list. At co...
-
7
[Python] Insert Line With Matched Pattern March 01, 2016 Extract URL...
-
4
Python program to insert a star between pair of identical charactersSkip to content
-
4
V2EX › Java Mybatis 可以直接 insert List<Map<String, String>> 数据吗
-
3
The list data type is one of the built-in data structures in Python along with sets, tuples, and dictionaries. You use a list to organize, group, and store data.But each of these data structures has distinctive features that differentiates...
-
7
PROBLEM OF THE DAY: 08/10/2023October 09, 2023 |220 ViewsPROBLEM OF THE DAY: 08/10/2023 | Insert in a Sorted ListProblem of the Day, linked-list, Data Structure and Algorithm Save ...
-
5
Sorted insert for circular linked listVideosFebruary 06, 2024 |720 ViewsPROBLEM OF THE DAY : 05/02/2024 | Sorted inser...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK