In this article, we will see how to insert at the front of a list in python.
Python has several built-in data types, list is one among those types. Using a list, we can store multiple values under a single name. Each value in the list is stored in a unique index.
To access an element from a list we need to use the index. The index range starts from 0 and ends with n – 1. Here, N is the no of elements present in the list.
To insert an element at the front of a list, we can use the insert() function and specify the position where we want to insert an element into a list.
Following is an example of how to insert an element at the front of a list.
# Create a list
fruits = ['apple', 'banana', 'watermelon', 'grapes']
print(fruits)
#insert an element at front of a list using the insert
fruits.insert(0, 'orange')
print(fruits)
#Again insert at front of a list
fruits.insert(0, 'citrus')
print(fruits)
Insertion at the end also works in a similar way. In the insert function, we need to specify the index where we need to insert. In this case, it is the length of the list.