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.

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) 

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.

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)

2.2 List insert() Parameters

  • position – Refers to the index position in which the element/iterable has to be inserted in the List.
  • iterable/ element – It can be an iterable type like List/Tuple/Set/Dictionary or a value of type int, string, e.t.c

3. Python List insert() Examples

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

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.

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

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.

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.

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'}]

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.

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.

References

Leave a Reply