How to Prepend to a List in Python [6 Methods]

Do you want to add elements to the beginning of a list? In this Python article, I will explain how to prepend to a list in Python using different methods with some illustrative examples.

To prepend to a list in Python, we can use various methods such as insert() for adding single elements, list concatenation or the * operator for multiple elements, list comprehension for complex operations, list slicing for a more flexible approach, and deque from the collections module for efficient prepending especially in large lists.

Methods to Prepend to a List in Python

There are six different methods to prepend to a list in Python:

  1. The insert() method
  2. Using List Concatenation
  3. The deque from the Collections Module
  4. Using List Comprehension (for Multiple Elements)
  5. The ‘*’ operator
  6. Using list slicing

Let’s see them one by one using some demonstrative examples:

1. Python prepend to list using insert() function

The insert() method is used to prepend to a list in Python. It allows us to insert an item at a specified index. To prepend, we would insert at index 0. For example:

us_cities = ['Los Angeles', 'Chicago', 'Houston']
us_cities.insert(0, 'New York')
print(us_cities)

Output:

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

The following screenshot illustrates the results of executing the code in the PyCharm editor.

Prepend to a List in Python

2. Pyhton prepend to a List using list concatenation

List concatenation involves combining two Python lists. We can prepend an item or multiple items to a list by concatenating a new list containing these items at the beginning of the original list.

For instance, to prepend to a list in Python:

route = ["Los Angeles", "Santa Barbara", "San Luis Obispo", "San Francisco"]
starting_city = ["San Diego"]
route = starting_city + route
print(route)

Output:

['San Diego', 'Los Angeles', 'Santa Barbara', 'San Luis Obispo', 'San Francisco']

Below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

Prepend to a Python a List

3. Python list prepend using the deque from the collections module

The collections module offers a deque (double-ended queue) object, designed to allow efficient appends and pops from both ends of the container. Using deque, we can prepend to a list in Python.

For example, to prepend to a list in Python:

from collections import deque

books = deque(["The Great Gatsby", "To Kill a Mockingbird"])
new_book = "1984"
books.appendleft(new_book)
print(list(books))

Output:

['1984', 'The Great Gatsby', 'To Kill a Mockingbird']

The screenshot below visually represents the output from running the code in PyCharm.

Prepend Lists In Python

4. Prepend in Python list using list comprehension

Python List comprehension offers a concise way to create lists. It can be used for prepending multiple elements to a list by iterating over the elements to be prepended and then extending them with the original list.

For instance, to prepend to a list in Python:

current_projects = ["Project X", "Project Y"]
new_projects = ["Project A Urgent", "Project B Urgent"]
current_projects = [project for project in new_projects] + current_projects
print(current_projects)

Output:

['Project A Urgent', 'Project B Urgent', 'Project X', 'Project Y']

The screenshot below showcases the output generated by running the code within the PyCharm editor.

Adding elements to the beginning of a Python list

5. Prepend to a short Python list using the * operator

The * operator in Python can be used to unpack elements from a list or other iterable, which can be helpful when we need to prepend multiple elements.

For example, to prepend to a list in Python:

basketball_teams = ['Celtics', 'Warriors']
basketball_teams = ['Lakers', 'Bulls', *basketball_teams]
print(basketball_teams)

Output:

['Lakers', 'Bulls', 'Celtics', 'Warriors']

Below is an image displaying the results of the code execution in the PyCharm environment.

python prepend to list

6. Prepend Python list using list slicing

To prepend to a list in Python using list slicing, we generally create a new list containing the items to be prepended and then use slicing to combine it with the original list. The slicing operation [:] creates a shallow copy of the list, ensuring that the original list is not modified.

For instance:

states_to_visit = ['Florida', 'New York', 'Illinois']
states_to_prepend = ['California', 'Texas']
states_to_visit = states_to_prepend + states_to_visit[:]
print(states_to_visit)

Output:

['California', 'Texas', 'Florida', 'New York', 'Illinois']

The ensuing image captures the results produced by executing the code in PyCharm.

How to Prepend to a List in Python

Conclusion

Here, I have explained how to prepend to a list in Python using six different methods like insert() function, list concatenation or list comprehension, the * operator, list slicing, or the deque class from the collections module, with some examples.

Understanding and choosing the right method for our specific use case can enhance the efficiency and readability of our code, making list manipulation a seamless aspect of Python programming.

You may also like to read: