In this tutorial, we will look at how to remove the first element from a Python list with the help of some examples.
How to remove the first element of a list?
You can use the python list pop()
function or apply a slice operation to remove the first element from a Python list. The following is the syntax for both the methods-
# remove first element using list pop() ls.pop(0) # remove first element using list slice ls = ls[1:]
Let’s now look at both these methods with the help of some examples.
Using list pop()
to remove the first element
The list pop()
function is used to remove an element from a list. Pass the index of the element you want to remove as the argument. It modifies the list in place and returns the element removed (or “popped”).
To remove the first element from a list, pass 0
as an argument to the list pop()
function. Here’s an example –
# create a list ls = [1, 2, 4, 7, 3] # remove first element ls.pop(0) # display the list print(ls)
Output:
[2, 4, 7, 3]
The list now has the first element from the original list removed.
Note that if the list is empty, the pop()
function will return an error.
Introductory ⭐
- 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
Intermediate ⭐⭐⭐
- 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
Advanced ⭐⭐⭐⭐⭐
- 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 ls = [] # remove first element ls.pop(0) # display the list print(ls)
Output:
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_5140/1061179126.py in <module> 2 ls = [] 3 # remove first element ----> 4 ls.pop(0) 5 # display the list 6 print(ls) IndexError: pop from empty list
We get an IndexError
error. This is because we’re trying to pop an element from an empty list.
Using list slice to remove the first element
Slice the list from the second element (the element at index 1
) to the end of the list and assign the resulting list to the original list. Let’s look at an example.
# create a list ls = [1, 2, 4, 7, 3] # remove first element ls = ls[1:] # display the list print(ls)
Output:
[2, 4, 7, 3]
We get the same result as above. Note that unlike the list pop()
function, which modifies the list in-place, here, we are creating a new list using a slice operation and then assigning it to our original list variable.
Also, note, you don’t get an error if you perform this slice operation on an empty list.
# create a list ls = [] # remove first element ls = ls[1:] # display the list print(ls)
Output:
[]
We don’t get any errors. The resulting list is also an empty list.
In this tutorial, we looked at the list pop()
function and the list slice operation to remove the first element from the list. You can use either of the two methods depending on your use case. Also, keep in mind the following points about these methods.
- The list
pop()
function modifies the list in place. It gives an error if applied on an empty list. - The list slice method returns a copy of the list with the element removed. It does not give an error when applied on an empty list.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.