Built with Tutify.io

    AIStudyLabs

    Python List insert() with Examples

    Python List insert() with Examples

    Python list.insert() method is used to insert an element to the list at a particular position, by using this you can insert an element or iterable at the first position, last position, or at any position. This method takes the integer as a position where you wanted to insert and the element to be inserted as arguments.

    Related:

    When you are using python List, you may have a requirement to insert an element/iterable at a particular position in the existing list. This article will discuss the insert() method syntax, parameters, and how to insert the element/iterable at a particular position by using different scenarios.

    <a id="h-1-quick-examples-of-list-insert"></a>

    1. Quick Examples of List insert()

    Following are quick examples of how to use the insert() method in python.

    
    # Quick Examples
    
    # Consider the list of strings
    mylist1=['hello','welcome','to','sparkby','examples']
    print("Actual List: ",mylist1)
    
    # Insert element at first position
    mylist1.insert(0,"java") 
    
    # Insert element at last position
    mylist1.insert(len(mylist1),"python") 
    
    # Insert list at 4th position
    mylist1.insert(3,["java",".net"])
    print(mylist1) 
    
    # Insert tuple to list
    mylist1.insert(3,("java",".net"))
    
    # Insert set
    mylist1.insert(3,{"java",".net"})
    print(mylist1) 
    
    # Insert dictionary of keys
    mylist1.insert(3,{"s1":"java","s2":".net"})
    print(mylist1) 
    
    # Insert dictionary of values
    mylist1.insert(3,{"s1":"java","s2":".net"}.values())
    print(mylist1)
    

    <a id="h-2-list-insert-method-in-python"></a>

    2. List insert() Method in Python

    Python list.insert() method is used to insert an element/iterable at a particular position. It will take two parameters. The first parameter is the index position and the second parameter is the element/iterable to be inserted. List/Set/Tuple/Dictionary can be iterable. It will consider the entire iterable as one element in the list after insertion.

    <a id="h-2-1-list-insert-syntax"></a>

    2.1 List insert() Syntax

    Let's look at the syntax of the list insert() method.

    
    # Here, mylist is the input list
    mylist.insert(position,iterable/element)
    

    <a id="h-2-2-list-insert-parameters"></a>

    2.2 List insert() Parameters

    <a id="h-3-python-list-insert-examples"></a>

    3. Python List insert() Examples

    We will see different scenarios of inserting elements into the list at different positions.

    <a id="h-3-1-insert-element-at-last-position"></a>

    3.1 Insert Element at Last Position

    If you want to insert an element at the last position of the python list, you can use the len() method to find the total number of elements in the list and use this as an index on the insert() method along with the element you wanted to insert.

    Let's create a list of strings and insert python at the last position on the list.

    
    # Consider the list of strings
    mylist1=['hello','welcome','to','sparkby','examples']
    print("Actual List: ",mylist1)
    
    # Insert element at last
    mylist1.insert(len(mylist1),"python") 
    print(mylist1) 
    
    # Output:
    # Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
    # ['hello', 'welcome', 'to', 'sparkby', 'examples', 'python']
    

    You can see that python is inserted at the last position in the list.

    <a id="h-3-2-insert-element-at-first-position"></a>

    3.2 Insert Element at First Position

    If you want to insert an element at the first position, you can directly specify the index as 0, as it refers to the starting position. Let's create a list of strings and insert java at the first position on the list.

    
    # Consider the list of strings
    mylist1=['hello','welcome','to','sparkby','examples']
    print("Actual List: ",mylist1)
    
    # Insert element at first
    mylist1.insert(0,"java") 
    print(mylist1) 
    
    # Output:
    # Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
    # ['java', 'hello', 'welcome', 'to', 'sparkby', 'examples']
    

    You can see that "java" is inserted at the first position in the list.

    4. Python Insert Iterables to List

    <a id="h-4-1-insert-tuple"></a>

    4.1 Insert Tuple

    Let's insert a tuple of elements at the third position in the existing list.

    
    # Consider the list of strings
    mylist1=['hello','welcome','to','sparkby','examples']
    print("Actual List: ",mylist1)
    
    # Insert tuple
    mylist1.insert(3,("java",".net"))
    print(mylist1) 
    
    # Output:
    # Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
    # ['hello', 'welcome', 'to', ('java', '.net'), 'sparkby', 'examples']
    

    We are considering a tuple that holds 2 strings. Now we inserted this tuple at the third index position. The list will consider this entire tuple as a single element.

    <a id="h-4-2-insert-list"></a>

    4.2 Insert List

    Let's insert a list of elements at the third position in the existing list.

    
    # Consider the list of strings
    mylist1=['hello','welcome','to','sparkby','examples']
    print("Actual List: ",mylist1)
    
    # Insert list
    mylist1.insert(3,["java",".net"])
    print(mylist1) 
    
    # Output:
    # Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
    # ['hello', 'welcome', 'to', ['java', '.net'], 'sparkby', 'examples']
    

    We are considering a list that holds 2 strings. We inserted this list at the third index position in the existing list.

    <a id="h-4-3-insert-set"></a>

    4.3 Insert Set

    Let's insert a set of elements at the fifth position in the existing list.

    
    # Consider the list of strings
    mylist1=['hello','welcome','to','sparkby','examples']
    print("Actual List: ",mylist1)
    
    # Insert set
    mylist1.insert(5,{"java",".net"})
    print(mylist1) 
    
    # Output:
    # Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
    # ['hello', 'welcome', 'to', 'sparkby', 'examples', {'java', '.net'}]
    

    <a id="h-4-4-insert-dictionary"></a>

    4.4 Insert Dictionary

    Let's insert Dictionary items at the second position in the existing list.

    
    # Consider the list of strings
    mylist1=['hello','welcome','to','sparkby','examples']
    print("Actual List: ",mylist1)
    
    # Insert dictionary
    mylist1.insert(2,{"s1":"java","s2":".net"})
    print(mylist1) 
    
    # Output:
    # Actual List:  ['hello', 'welcome', 'to', 'sparkby', 'examples']
    # ['hello', 'welcome', {'s1': 'java', 's2': '.net'}, 'to', 'sparkby', 'examples']
    

    In the above code, we inserted the dictionary that holds two items at the second index position in the list.

    <a id="h-4-conclusion"></a>

    4. Conclusion

    This article shows different ways to insert elements at different positions on the python list. insert() method is used to insert an element/iterable at a particular position. For better understanding, we explained inserting an element to the list, iterable to the existing list. You can also insert nested data structures like the list of tuples/ list of lists etc to the list using the insert() method.

    <a id="h-related-articles"></a>

    Related Articles

    References