How to Add an Element to the Beginning of a List in Python?

As a data scientist working with large datasets of U.S. cities, I recently encountered a situation where I needed to prepend a new city to an existing list. After researching various methods, I discovered several effective ways to accomplish this task. In this tutorial, I will explain how to add an element to the beginning of a list in Python with examples.

Python Lists

Before getting into the solutions, let’s briefly review Python lists. In Python, a list is an ordered, mutable, and versatile data structure that allows you to store and manipulate collections of elements. Lists are defined using square brackets [] and can contain elements of different data types.

For example, let’s say we have a list of major U.S. cities:

cities = ["New York", "Los Angeles", "Chicago", "Houston"]

Read How to Count the Frequency of Elements in a Python List?

Add an Element to the Beginning of a List in Python

To add an element to the front of a list in Python, you can use the insert() method with an index of 0. For example, if you have a list cities = ["New York", "Los Angeles", "Chicago"] and you want to add “Boston” to the beginning, you can use cities.insert(0, "Boston") resulting in ["Boston", "New York", "Los Angeles", "Chicago"].

Python provides multiple ways to add an element to the front of a list. Let’s explore each method in detail.

1. Use the insert() Method

The insert() method in Python is a built-in list method that allows you to insert an element at a specified index. To add an element to the beginning of the list, we can use insert() with an index of 0.

Example:

cities = ["New York", "Los Angeles", "Chicago", "Houston"]
cities.insert(0, "Philadelphia")
print(cities)

Output:

['Philadelphia', 'New York', 'Los Angeles', 'Chicago', 'Houston']

You can see the output in the screenshot below.

Add an Element to the Beginning of a List in Python

In this example, we insert the city “Philadelphia” at index 0, effectively adding it to the front of the cities list.

Check out How to Convert Dictionary to List of Tuples in Python?

2. Use the + Operator

Another way to add an element to the front of a list is by using the + operator in Python. We can create a new list with the element we want to add and concatenate it with the original list.

Example:

cities = ["New York", "Los Angeles", "Chicago", "Houston"]
cities = ["Boston"] + cities
print(cities)

Output:

['Boston', 'New York', 'Los Angeles', 'Chicago', 'Houston']

You can see the output in the screenshot below.

How to Add an Element to the Beginning of a List in Python

Here, we create a new list ["Boston"] and concatenate it with the cities list using the + operator. The resulting list has “Boston” added to the front.

Read How to Split a Python List Into Evenly Sized Chunks?

3. Use Iterable Unpacking

Iterable unpacking, introduced in Python 3.5, provides a concise way to add elements to the front of a list. We can use the * operator to unpack the original list and combine it with the new element.

Example:

cities = ["New York", "Los Angeles", "Chicago", "Houston"]
cities = ["Seattle", *cities]
print(cities)

Output:

['Seattle', 'New York', 'Los Angeles', 'Chicago', 'Houston']

You can see the output in the screenshot below.

Add an Element to the Beginning of a List in Python iterable unpacking

In this example, we create a new list ["Seattle", *cities], where *cities unpacks the elements of the original cities list. The resulting list has “Seattle” added to the front.

Check out How to Find the Index of the Maximum Value in a List using Python?

Example

Let’s consider a real-world scenario where adding an element to the front of a list is useful. Suppose you are analyzing population data for major U.S. cities. You have a list of cities sorted by population in descending order:

cities_by_population = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]

Later, you receive updated data showing that Philadelphia has surpassed Phoenix in population. To reflect this change, you need to add Philadelphia to the front of the cities_by_population list.

Using the insert() method:

cities_by_population.insert(0, "Philadelphia")
print(cities_by_population)

Output:

['Philadelphia', 'New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']

Now, the cities_by_population list accurately represents the updated population rankings.

Read How to Capitalize the First Letter of Every Word in a List using Python?

Conclusion

In this tutorial, I explained three methods to add an element to the beginning of a list in Python using the insert() method, the + operator, and iterable unpacking. Each method has its advantages and use cases. I also discussed a real-world example.

You may like to 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.