5 Ways to Prepend an Element to the Front of a List in Python

In this article, you will learn how to add an element to the front of a list in Python.

To prepend an element to the front of a Python list you can utilize different techniques. The most straightforward approach is to use the list insert() method. You can achieve the same result also using list concatenation, the list extend() method, the unpacking operator, and list slicing.

The most common scenario is trying to append elements to the end of a list, but what if you want to add something to the beginning?

Let’s write the Python code to do this in a number of ways!

Note: the terms “element” of a list and “item” of a list are used in this article with the same meaning.

Why Add an Element to the Front of a Python List?

Before learning how to write the code to add to the front of a list in Python, let’s understand why you might want to do that in your program.

Imagine you are building a CMS (Content Management System) that displays articles that have been published starting from the most recent article. You would add the most recent article as the first element of a list of articles and then the program would display all the elements in the list starting from the first.

Note: In this example, we are assuming you are using a list as a simple data structure and this might not be the case in a real program considering that Python also provides a more flexible data structure like the dictionary.

There are other use cases in which you would add an item to the beginning of a list, for example, if you are building a stack in Python.

How Do You Insert an Item to the Front of a List in Python?

The list insert() method allows you to insert a value at any index of a Python list. To add the value to the beginning of a list you can use the insert() method and pass to it the index 0. The idea is that we want to insert the item in the first position of the list.

Here is the syntax of the list insert() method in Python:

list.insert(i, element)

The following table explains the role of the two arguments passed to the insert() method:

ArgumentDescription
iThe first argument is the index of the element in the list before which to insert the new element.
elementThe second argument is the element you want to insert in the list.

The code below shows how to use the insert() method in practice:

numbers = [7, 8, 9]
numbers.insert(0, 6)
print(numbers)

This is the output of the print statement:

[6, 7, 8, 9]

As expected, we have added the element to the beginning of the list by using the following syntax. You can see that we are passing index 0 to the insert() method:

list.insert(0, element)

Using List Concatenation to Add an Element to the Beginning of a List

Let’s explore another way to solve the same problem.

To add an element to the beginning of a list you can use list concatenation. You can make a new list that only contains the element you want to add at the beginning of the first list and then concatenate the two lists together. You can use the + operator to concatenate the two lists.

This method can be less intuitive than the previous one, but understanding it is very useful for you to get familiar with the way Python works with different data types.

numbers = [7, 8, 9]
numbers = [6] + numbers
print(numbers)

On the second line of code, we create a new list with one element and then use the concatenation operator (+) to join that list with the numbers list. In other words, we are concatenating two lists together.

How Do You Add an Item at the Front of a List Using the extend() Method?

Are there any other ways to add items at the front of a list in Python?

If you have a list, the list extend() method is generally used to append multiple elements to the end of the list. The extend() method can also be used to prepend a single element to the front of a list but it requires more lines of code.

Let’s see how to update the Python code we have created in the previous examples to use the extend() method instead.

By looking at the number of lines of code we are using in the example below, you will understand that the extend() method is not really designed to add an element to the beginning of lists:

numbers = [7, 8, 9]
temp_numbers = [6]
temp_numbers.extend(numbers)
numbers = temp_numbers
print(numbers)

Confirm that the output of this code is the one we expect:

[6, 7, 8, 9]

Yes, it is!

In the code above you are first creating a temporary list with one element that is the element to add to the front of the numbers list. Using extend() we create the full list (temp_numbers variable) before reassigning it to the numbers list.

When writing code always remember to keep it concise so this approach is definitely not one to go for. It’s just useful to understand how the list extend() method works.

How Do You Insert an Element to the First Position in a List Using the Unpacking Operator?

Python’s unpacking operator (*) can be used to add an element in the first position of a list.

You will see that this is definitely a more concise approach than the previous one.

But before writing the final Python code let’s open the Python shell to see how the unpacking operator works:

>>> numbers = [7, 8, 9]
>>> [*numbers]
[7, 8, 9]

Now that you know what unpacking looks like and what output it returns, let’s see how to use it to add an item in the first position of a Python list:

numbers = [7, 8, 9]
numbers = [6, *numbers]
print(numbers)

The output of this code is correct. You can see the integer 6 before the elements of the original list:

[6, 7, 8, 9]

This approach is definitely concise and is one of the most Pythonic ones we have covered in this article.

At the same time, it might not be beginner-friendly considering that unpacking a list is not a concept you would learn when you are just starting with Python.

Can You Prepend an Item to a Python List Using Slicing?

Let’s explore the last way to insert an item at the beginning of an existing list.

Using slicing you can specify where in a list to insert an element. For example, to insert an element before the first element you can use the slice syntax list[:0].

Here is the Python code that uses slicing to add an item at the start of a list:

numbers = [7, 8, 9]
numbers[:0] = [6]
print(numbers)

Let’s verify that the output is correct before doing a recap of how this approach works:

[6, 7, 8, 9]

Yes, the output is correct.

In the code, we have just written, the following expression is used to extend the slice of the list before the element in position 0 with the list [6]. The result is that we are adding element 6 to the front of the list.

numbers[:0] = [6] 

This approach works but it’s not very readable and user-friendly especially if you are just starting with Python programming.

Also, even if you are an experienced Python developer always remember to write code that is easily readable so other developers can understand it quickly.

The expression we have just seen above where we assign a list of one element to a slice of a list can be hard to read for someone who has to work with that line of code for the first time.

Conclusion

Now you know five different ways to add an item to the front of a list in Python.

Every approach has its pros and cons and you can choose the one that fits the best based on the application you are writing.

The first approach that uses the insert() method can be a good candidate for the best method considering its simplicity, readability, and the fact that it requires only a single line of code.

Related article: After learning how to add an element to the beginning of a list, it’s important for you to cover the basics of adding items to the end of a Python list.

Leave a Comment