Python Insert Element at Specified Index in List

Add/insert element at particular index in list python; Through this tutorial, i am going to show you how to add element at particular index in list python.

Python Insert Element at Specified Index in List

Now, i will write a python program to insert an element at a specified position into a given list; as shown below:

  • 1 – Inserting an element in list at specific index using list.insert()
  • 2 – Add element at beginning of list python
  • 3 – Python program to insert an element at a specific position into second list

1 – Inserting an element in list at specific index using list.insert()

For inserting an element in list at a particular/specific index/position, python provides function name insert().

Syntax of index() function:

 list.insert(position, element) 

It accepts two parameters, first is the position of the element and second one is element to inserts the element at a given position in the list.

Here, we will take several examples of index() function, Let’s see an examples,

Assume we have simple list of strings i.e.

 List of string list1 = ['Hi' ,  'hello', 'at', 'this', 'there', 'from'] 

Now let insert ‘world’ at 3rd position in the list i.e.

 # Add an element at 3rd position in the 
listlist1.insert(3, 'world') 

Index will always start from 0 in list. So, element will be inserted at 3rd position i.e. after 0,1 & 2.

So, list contents will be now,

 ['Hi', 'hello', 'at', 'world', 'this', 'there', 'from'] 

2 – Add element at beginning of list python

To insert the element at the front of above list, call insert() function i.e.

 # Add an element at the front of 
listlist1.insert(0, 'my') 

So, list contents will be now,

 ['my', 'Hi', 'hello', 'at', 'world', 'this', 'there', 'from'] 

3 – Python program to insert an element at a specific position into second list

Suppose we have two lists i.e.

 list1 = ['my', 'Hi', 'hello', 'at', 'world', 'this', 'there', 'from'] 
 list2 = [3,5,7,1] 

Now Insert all the elements of list2 at 3rd position in list1

Method 1:

Iterate over list2 in reverse and keep on inserting element at 3rd index in list1 using list.insert() i.e.

#Insert all the elements in list2 to list1 between 3 to 4 th element
 
for elem in reversed(list2) :
 list1.insert(3, elem)

Method 2:

Splice list1 from 0 to 2 and merge all elements of list2 in it. Then merge all the remaining elements of list from 3 to end i.e.

 # Insert all the elements in list2 to list1 between 3 to 4 th 
 elementlist1 = list1[:3] + list2 + list1[3:] 

In both cases lists content will be now,

 ['my', 'Hi', 'hello', 3, 5, 7, 1, 'at', 'world', 'this', 'there', 'from'] 

The given below python program contain all the above examples:

'''
Inserting all elements of list1 at specific index in other list
'''
def main():
# List of string 
list1 = ['Hi' ,  'hello', 'at', 'this', 'there', 'from']
# Print the List
print(list1)
# Add an element at 3rd position in the list
list1.insert(3, 'world')
# Print the List
print(list1)
# Add an element at the front of list
list1.insert(0, 'my')
# Print the List
print(list1)
list2 = [3,5,7,1]
# Insert all the elements in list2 to list1 between 3 to 4 th element
for elem in reversed(list2) :
list1.insert(3, elem)
# Print the List
print(list1)    
# List of string 
list1 = ['Hi' ,  'hello', 'at', 'this', 'there', 'from']
# Insert all the elements in list2 to list1 between 3 to 4 th element
list1 = list1[:3] + list2 + list1[3:]
# Print the List
print(list1)    
if __name__ == '__main__':
main()

Output:

 ['Hi', 'hello', 'at', 'this', 'there', 'from']
 ['Hi', 'hello', 'at', 'world', 'this', 'there', 'from']
 ['my', 'Hi', 'hello', 'at', 'world', 'this', 'there', 'from']
 ['my', 'Hi', 'hello', 3, 5, 7, 1, 'at', 'world', 'this', 'there', 'from']
 ['Hi', 'hello', 'at', 3, 5, 7, 1, 'this', 'there', 'from']

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *