Skip to Content

Python – Remove First Element From List

In this tutorial, we will look at how to remove the first element from a Python list with the help of some examples.

remove first element from a python 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.

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.

# 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.

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.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.