Prepend Elements to Lists in Python

Recently, I was working on a project where I needed to add elements to the beginning of a Python list instead of the end.

The challenge was simple: Python lists have an append() method to add items at the end, but there is no built-in prepend() method. So, I had to explore different approaches to achieve this.

In this tutorial, I’ll share six practical ways I use to prepend elements to a Python list. These are methods I’ve applied in real-world projects over the years as a Python developer.

Why Prepend Instead of Append?

Most of the time, we add items to the end of a list using append(). But there are situations where you need to add items at the front:

  • When processing a queue where new tasks must be prioritized.
  • When working with time-series data and you want the latest entry at the start.
  • When building a custom log where the newest message should appear first.

In these cases, prepending is essential.

Method 1 – Use insert() Method

The simplest way to prepend an element is by using the Python’s insert() method. This method allows you to insert an item at a specific index. To put something at the beginning, you pass 0 as the index.

# Prepending using insert()
my_list = [10, 20, 30]
print("Original List:", my_list)

# Insert at the beginning
my_list.insert(0, 5)
print("After Prepending:", my_list)

Output:

Original List: [10, 20, 30]
After Prepending: [5, 10, 20, 30]

I executed the above example code and added the screenshot below.

python list prepend

I often use this method when I only need to add one element. It’s quick and easy.

Method 2 – Use List Concatenation

Another way is to create a new list and concatenate it with the existing one.

# Prepending using concatenation
my_list = [10, 20, 30]
print("Original List:", my_list)

# Prepend element 5
my_list = [5] + my_list
print("After Prepending:", my_list)

Output:

Original List: [10, 20, 30]
After Prepending: [5, 10, 20, 30]

I executed the above example code and added the screenshot below.

how to prepend to a list in python

This works well if you want to prepend multiple elements at once.

# Prepending multiple elements
my_list = [10, 20, 30]
my_list = [1, 2, 3] + my_list
print(my_list)

Output:

[1, 2, 3, 10, 20, 30]

The downside is that it creates a new list, which might not be efficient for very large datasets.

Method 3 – Use List Slicing

Python List slicing is another clean way to prepend elements.

# Prepending using slicing
my_list = [10, 20, 30]
print("Original List:", my_list)

# Prepend 5
my_list[:0] = [5]
print("After Prepending:", my_list)

Output:

Original List: [10, 20, 30]
After Prepending: [5, 10, 20, 30]

I executed the above example code and added the screenshot below.

python prepend to list

I like this method because it modifies the list in place without creating a new one.

Method 4 – Use collections.deque

If you need to prepend elements frequently, Python’s deque (double-ended queue) is the most efficient choice.

from collections import deque

# Prepending using deque
my_list = deque([10, 20, 30])
print("Original Deque:", my_list)

# Prepend 5
my_list.appendleft(5)
print("After Prepending:", my_list)

Output:

Original Deque: deque([10, 20, 30])
After Prepending: deque([5, 10, 20, 30])

You can always convert it back to a list:

my_list = list(my_list)
print("Converted to List:", my_list)

This is my go-to method when performance matters, especially in queue-like applications.

Method 5 – Use List Comprehension

Sometimes, I use list comprehension when I need to prepend elements dynamically.

# Prepending using list comprehension
my_list = [10, 20, 30]
print("Original List:", my_list)

# Prepend 5
my_list = [x for x in [5] + my_list]
print("After Prepending:", my_list)

While this looks similar to concatenation, list comprehension gives me flexibility if I want to apply transformations while prepending.

Method 6 – Use * Operator (Unpacking)

Python’s unpacking operator * can also be used to prepend elements.

# Prepending using unpacking
my_list = [10, 20, 30]
print("Original List:", my_list)

# Prepend 5
my_list = [5, *my_list]
print("After Prepending:", my_list)

Output:

Original List: [10, 20, 30]
After Prepending: [5, 10, 20, 30]

This method is concise and works great when you want to combine lists dynamically.

Which Method Should You Use?

  • Use insert(0, element) for single elements.
  • Use concatenation or unpacking when prepending multiple elements.
  • Use deque when performance is critical.
  • Use slicing if you prefer in-place operations.

Each method has its use case, and I often switch depending on the project.

Real-World Example – Prepend Daily Sales Data

Let’s say you are analyzing daily sales for a U.S. retail store. You receive the latest sales number every morning, and you want to keep the newest entry at the beginning of your list.

# Daily sales data (oldest to newest)
sales = [200, 250, 300, 280]

# New sales data for today
today_sales = 310

# Prepend today's sales
sales.insert(0, today_sales)

print("Sales Data:", sales)

Output:

Sales Data: [310, 200, 250, 300, 280]

This way, the most recent sales numbers always appear first. While Python doesn’t provide a direct prepend() method, we have multiple ways to achieve the same result.

For small lists, insert() or concatenation works perfectly. For larger or performance-critical applications, deque is the best choice.

I encourage you to try these methods in your projects. Once you practice them, you’ll know exactly which one to use in different situations.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.