Lists are one of the most important data structures in Python. They store a collection of items, which can be of any data type. Lists are mutable, which means that you can change their contents by adding, removing, or modifying elements. Lists are also ordered, meaning there is an order to the elements present inside a list. There are times when you may need to add elements to the beginning of the list, which is known as prepending. In this tutorial, we will explore how to prepend elements to a list in Python with relevant code and examples.
Knowing how to perform operations like prepend on a list can be very helpful in certain situations, such as when you need to maintain the order of the elements in the list or when you need to implement a stack or queue data structure.
Visualization of prepending an item to a list
The following image shows what it means to prepend an element (or item) to a list.
You can see that we initially have the list [1, 2, 3] to which we prepend the value 4 such that the resulting list now becomes [4, 1, 2, 3]. Notice that when prepending we’re actually adding (or inserting) the item at the beginning of the list.
Methods to prepend an element to a list in Python
We have seen what it means to prepend an element to a list. Let’s now look at how we can implement the prepend operation on a Python list.
Method 1 – Using the
You can use the Python list built-in
insert() function to prepend an element to a list. The
insert() function is used to insert an element at a given index in a list. Since a list is a mutable object, the element gets inserted at the specified index and all the following elements get shifted one position to the right. The following is the syntax –
This function inserts the specified
element at the specified
index in the list
my_list. All the elements after the inserted element are shifted to the right. If the
index is greater than or equal to the length of
element is appended to the end of the list.
Let’s look at an example.
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# create a list my_list = [1, 2, 3] # prepend 4 to the list my_list.insert(0, 4) # display the list print(my_list)
[4, 1, 2, 3]
Here, we prepended the element 4 to the list containing 1, 2, and 3. Note that the
insert() function modifies the list in place. The list now has the new element 4 at the start.
Method 2 – Using the
The addition operator,
+ can be used to concatenate two or more lists together. To prepend an element using the
+ operator, you can use the following syntax –
my_list = [element] + my_list
Here, we want to prepend the specified
element to the list,
my_list. Notice that we’re enclosing
element in square brackets, this is done so that we get a list of one element which is then concatenated with the list
my_list, and the resulting list is stored back in the same variable
my_list (you can choose a separate variable if you’d like).
Let’s look at an example.
# create a list my_list = [1, 2, 3] # prepend 4 to the list my_list =  + my_list # display the list print(my_list)
[4, 1, 2, 3]
You can see that we get the correct result. The element 4 is prepended to the list.
Note that there may be other methods as well but the above two are quite straightforward.
Difference between prepend and append
Prepending an item to a list in Python means adding an element to the beginning of the list. As we saw in this tutorial, this can be done using the
insert() method or the
+ operator. For example, if we have a list
my_list = [1, 2, 3], and we want to prepend the value
0 to it, we can use either of the following:
# prepend using the insert method my_list.insert(0, 0) # prepend using the + operator my_list =  + my_list
On the other hand, appending an item to a list in Python means adding an element to the end of the list. This can be done using the
append() method. It takes one argument, which is the element to be added to the list. The original list is modified and the new element is added at the end of the list. For example, if we have a list
my_list = [1, 2, 3], and we want to append the value
4 to it, we can use the following:
# append using the append method my_list.append(4)
Note that you can also use the
+ operator here to append an element to a list.
In this tutorial, we looked at what it means to prepend an element to a list and how to do it in Python using different methods with the help of some examples. The following are the key takeaways –
- Prepending an element to a list means, adding the element to the beginning of the list.
- You can prepend an element to a list in Python using the
insert()method and also using the
You might also be interested in –