You are currently viewing Add Element to Front of List in Python

How to add elements to the front of the list in Python? You can add an element to the start or front of a list in Python, you can use the + operator, insert(), slicing, unpacking operator, collections.deque.appendleft(), and extend() function. In this article, I will explain adding elements to front a list in python by using all these methods with examples.

Advertisements

1. Quick Examples of Adding Items to the Front of a List

If you are in a hurry, below are some quick examples of adding elements to the front of a list in python.


# Quick examples of adding items to the front of a list

# Example 1: Add element at front 
# Using insert()
technology = ['Spark','Pandas','Pyspark']
technology.insert(0, 'Python')

# Example 2: Using [] and + operator
numbers = [6, 7, 9, 12]
result = [3] + numbers

# Example 3: Iterable unpacking operator
number = 7
number1 = [19, 22, 40, 56, 78]
result = [number, *number1]

# Example 4: Add element to front of list 
# Using Slicing
technology = ['Spark', 'Python','Pandas']
technology[:0] = ['Hadoop']

# Example 5: Add element to front of list 
# Using extend() method
technologys = ['Spark', 'Python','Pandas']
technology=['Java']
technology.extend(technologys)

# Example 6: Using collections.deque.pushleft()
technology = ['Spark', 'Python','Pandas']
technology= deque(technology)
technology.appendleft('Hyperion')
technology = list(technology)

2. Add Element to Front of List Using insert()

You can use the list.insert() to add an element to the front of a list in Python, all you need to do is just pass the 0 as the first argument and the value you wanted to add as a second param. This method is used to add an element by index to python list.

In the below example, technology.insert(0,'Python') inserts the element 'Python' at the beginning of the technology list, shifting the existing elements to the right. The below code adds the ‘Python' at the first position on the list.


# Consider list
technology = ['Spark','Pandas','Pyspark']
print('Original list:',technology)

# Add element to front of list 
# Using insert()
technology.insert(0, 'Python')
print('Updated list:',technology)

Yields below output.

python list add front

Similarly, you can use the insert() method to add an element to the front of a list in Python. For instance, numbers.insert(0, 2) inserts the element 2 at index 0, effectively adding it to the front of the list. After running this code, the numbers will be [2, 4, 7, 9].


# Consider list with numbers
numbers = [4, 7, 9]
print('Original list:',numbers)

# Insert 2 at first position
numbers.insert(0, 2)
print('Updated list:',numbers)

Yields below output.

python list add front

3. Add Element to Front of List Using + Operator

You can use the "+" operator to concatenate two lists together. Use the element you wanted to add as a left operand within square brackets.

In the below example, [3] + numbers create a new list where the element 3 is concatenated with the original numbers list, effectively adding it to the front of the list. Keep in mind that this approach creates a new list, unlike the insert() method, which modifies the list in place.


# Consider list
numbers = [6, 7, 9, 12]
print('Original list:',numbers)

# Using [] and + operator
result = [3] + numbers
print('Updated list:', result)

# Output:
# Original list: [6, 7, 9, 12]
# Updated list: [3, 6, 7, 9, 12]

4. Using Iterable Unpacking Operator

Alternatively, you can use the iterable unpacking operator to add an element to the front of a Python list. Here, create a new list that includes the element followed by the elements of the original list. For example, *number1 in the line result=[number, *number1] expands the elements of number1 within the new list result, effectively adding the elements of number1 after the value of number. The resulting list is [7, 19, 22, 40, 56, 78].


# Consider list
number1 = [19, 22, 40, 56, 78]
print('Original list:',number1)

# Iterable unpacking operator
number = 7
result = [number, *number1]
print('Updated list:', result)

# Output
# Original list: [19, 22, 40, 56, 78]
# Updated list: [7, 19, 22, 40, 56, 78]

5. Add Element to Front of List Using Slicing

You can use slicing to insert an element at the front of a list by assigning the result of slicing to a new list that includes the element. You can assign the [:0] sliced lists to the list converted from the element.

In the below example, technology[:0] = ['Hadoop'] uses slicing to insert the elements from ['Hadoop'] at the beginning of the technology list. The original list is modified in place, and the final list becomes ['Hadoop', 'Spark', 'Python', 'Pandas'].


# Consider the list of strings
technology = ['Spark', 'Python','Pandas']
print("Actual List: ",technology)

# Using slicing to add at front
technology[:0] = ['Hadoop']
print("Final List: ",technology)

# Output:
# Actual List:  ['Spark', 'Python', 'Pandas']
# Final List:  ['Hadoop', 'Spark', 'Python', 'Pandas']

6. Add Element to Front of List Using extend() Method

Similarly, you can add an element to the front of a list using the extend() method, you can create a new list that includes the element and then extend the original list with the new list. By using this you can also join multiple Python lists.


# Consider list
technologys = ['Spark', 'Python','Pandas']
print("Actual List: ",technologys)

# Using extend() method
technology=['Java']
technology.extend(technologys)
print("Final List: ",technology)

# Output:
# Actual List:  ['Spark', 'Python', 'Pandas']
# Final List:  ['Java', 'Spark', 'Python', 'Pandas']

7. Using collections.deque.appendleft() Method

The collections.deque.appendleft() method can be used to add an element to the front of a list-like object by creating a deque and using the appendleft() method to insert the element at the left end of the deque.

This code creates a deque from the original list, adds the element 'Hyperion' to the front of the deque using appendleft(), and then converts the deque back to a list for display purposes.


from collections import deque

# Consider the list of strings
technology = ['Spark', 'Python','Pandas']
print("Actual List: ",technology)

# Using collections.deque.pushleft()
# Add element to front of list
technology= deque(technology)
technology.appendleft('Hyperion')
technology = list(technology)
print("Final List: ",technology)

# Output:
# Actual List:  ['Spark', 'Python', 'Pandas']
# Final List:  ['Hyperion', 'Spark', 'Python', 'Pandas']

Frequently Asked Questions on Add Element to Front of List

How can I add an element to the front of a list in Python?

There are multiple ways to add an element to the front of a list. You can use the insert() method, the + operator, or iterable unpacking (*). Another option is to use slicing or the deque class from the collections module.

Can I use the insert() method to add an element to the front of a list?

You can use the insert() method to add an element to the front of a list in Python. The insert() method allows you to insert an element at a specified position in a list. To add an element to the front, you would set the index to 0.

How do I use the + operator to add an element to the front of a list?

You can use the + operator to add an element to the front of a list by creating a new list that contains the element followed by the elements of the original list.

What is iterable unpacking, and how can it be used to add an element to the front of a list?

Iterable unpacking is a feature in Python that allows you to unpack the elements of an iterable (like a list, tuple, or string) into individual elements. It is denoted by the asterisk (*) operator. To add an element to the front of a list using iterable unpacking, you can create a new list by unpacking the original list and including the new element.

How does slicing help in adding an element to the front of a list?

Slicing allows you to create a new list by extracting a portion of an existing list. To add an element to the front of a list using slicing, you can slice the list at index 0 and then concatenate it with a list containing the new element.

Can the collections.deque class be used to add an element to the front of a list?

The collections.deque class can be used to efficiently add an element to the front of a sequence (including a list) using the appendleft() method.

Conclusion

In this article, I have explained how to add an element to the front of a list in Python by using the + operator, insert(), slicing, unpacking operator, collections.deque.appendleft(), and extend() functions with examples.

Happy Learning !!