Python : How to Insert an element at specific index in List ?

In this article we will discuss how to insert an element in list at specific position.

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

In python list provides a member function insert() i.e.

list.insert(position, element)

It accepts a position and an element and inserts the element at given position in the list.

Let’s see an example,

Suppose we have a list of strings i.e.

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

Now let insert ‘why’ at 3rd position in the list i.e.
# Add an element at 3rd position in the list
list1.insert(3, 'why')

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

Advertisements

So, list contents will be now,

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

Inserts an element at beginning of list

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

# Add an element at the front of list
list1.insert(0, 'city')

So, list contents will be now,
['city', 'Hi', 'hello', 'at', 'why', 'this', 'there', 'from']

Insert all elements of another list at specific index in given list

Suppose we have two lists i.e.

list1 = ['city', 'Hi', 'hello', 'at', 'why', '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 element
list1 = list1[:3] + list2 + list1[3:]

In both cases lists content will be now,
['city', 'Hi', 'hello', 3, 5, 7, 1, 'at', 'why', 'this', 'there', 'from']

Complete example is as follows,
'''
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, 'why')
    
    # Print the List
    print(list1)
    
    
    # Add an element at the front of list
    list1.insert(0, 'city')
    
    # 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', 'why', 'this', 'there', 'from']
['city', 'Hi', 'hello', 'at', 'why', 'this', 'there', 'from']
['city', 'Hi', 'hello', 3, 5, 7, 1, 'at', 'why', 'this', 'there', 'from']
['Hi', 'hello', 'at', 3, 5, 7, 1, 'this', 'there', 'from']

 

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top