Python

How to Insert in the Front Index List in Python

In this tutorial, we are going to see how we can insert an element at the front of the list in Python. In addition, we will only use the integer concepts to be easy to understand, and other data types will be similar, as shown in this article. So, we are going to discuss the following methods:

  1. List.Insert Function
  2. Addition Operator Method
  3. Slice Method
  4. Unpacking Iterable Method

So, let’s discuss each method in detail.

Method 1: Using list.insert Function

The first method which we are going to discuss is the insert function. This function accepts two parameters: index_position and value. In the following code provided, we are inserting variable val at the index position 0:

1
2
3
4
5
6
7
8
9
#python list_insert_method.py

if __name__ == '__main__':

    lst = [12, 33, 45, 56,47]
    val = 1

    lst.insert(0, val)
    print(lst)  # prints [1, 12, 33, 45, 56, 47]

Output: python list_insert_method.py

[1, 12, 33, 45, 56, 47]

Line 5: We created a list of integers (lst).

Line 6: We created a variable val and assigned it to value 1, which we want to insert at the front of the list.

Line 8: Now, we call the method insert and pass two parameters (index_position, value). The first parameter is the index_position, where we want to insert the value, so we keep it 0. Another parameter is a value that we want to insert at the index_position 0.

Line 9: The print statement shows that our val is successfully inserted at the front of the list.

Method 2: Using Addition Operator (+) Method

In this method, we are going to change the data type of the value from integer to list, which we want to insert at the front position of the list. After that, we use the addition operator and merge the first list item, which is a value, into the other list, as shown below in the program:

1
2
3
4
5
6
7
8
9
#python addition_operator_method.py

if __name__ == '__main__':

    lst = [12, 33, 45, 56,47]
    val = 1

    lst = [val] + lst
    print(lst)  # prints [1, 12, 33, 45, 56, 47]

Output: python addition_operator_method.py

[1, 12, 33, 45, 56, 47]

Line 8: We transform the value into the list and then, use the addition operator to append the first list in front of the second list.

Line 9: The print statement shows that our val is successfully inserted at the front of the list.

Method 3: Using Slice Method

The other method which we are going to discuss is the slice method. As we know, list[:1] will print the element at index position 0, and list[:2] will print two values at the index position 0 and 1. So whatever value we pass in the slice list[:n] will print (n -1) elements. So, if we pass list[:0], then it will show as empty because there is no such index value in the list. If we insert the element in that particular index, it will append at the front of the list as shown in the following program:

1
2
3
4
5
6
7
8
9
# python slice_method.py

if __name__ == '__main__':

    lst = [12, 33, 45, 56,47]
    val = 1
    print(lst[:0])
    lst[:0] = [val]
    print(lst)  # prints [1, 12, 33, 45, 56, 47]

Output: python slice_method.py

[]
[1, 12, 33, 45, 56, 47]

Line 7: We print the lst[ :0] to check what values we will get. From the output, we can clearly say that it is print [ ] empty list.

Line 8: We assigned the list val to the empty list as explained above.

Line 9: The print statement shows that our val is successfully inserted at the front of the list.

Method 4: Using Unpacking Iterable Method

The last method which we are going to discuss is the asterisk (*) operator. This is an iterable method, and also, it inserts the value at the front of the list.

1
2
3
4
5
6
7
8
9
# python unpacking_operator_method.py

if __name__ == '__main__':

    lst = [12, 33, 45, 56,47]
    val = 1
    print(*lst)
    lst = [val, *lst]
    print(lst)  # prints [1, 12, 33, 45, 56, 47]

Output: python unpacking_operator_method.py

12 33 45 56 47
[1, 12, 33, 45, 56, 47]

Line 7: When we print the asterisk list, we get all the elements as shown in the output. Thus, we can say that it is iterable.

Line 8: We kept val and *list together in a list and then, assigned it to another list.

Line 9: The print statement shows that our val is successfully inserted at the front of the list.

Conclusion

We have shown you the different methods to add an element at the front of the list. We have seen different methods, such as Insert, Addition (+) Operator, Slice, and Unpacking Asterisk. All the methods are very easy to use, but along with that, we also have to care about the processing time. Because if the data is less, we may not care about it. However, if we have billions of data, then it might matter. Based on this premise, the Insert Method is the best method in processing.

This tutorial code is available at the Github link.

About the author

Shekhar Pandey