In Python, we can remove the first element from a list easily – there are many ways to remove the first item from a list using Python.

The easiest way to remove the first element from a list is with slicing.

list = [1,2,9,0,1,3]

list_without_first_element = list[1:]

print(list_without_first_element)

#Output:
[2,9,0,1,3]

Another method to remove the first item in a list is with the Python pop() function.

list = [1,2,9,0,1,3]

print(list.pop(0))

#Output:
[2,9,0,1,3]

You can also use the Python del keyword to delete the first element from a list.

list = [1,2,9,0,1,3]

del list[0]

print(list)

#Output:
[2,9,0,1,3]

You can also use the Python remove() function to remove the first element of a list.

list = [1,2,9,0,1,3]

list.remove(list[0])

print(list)

#Output:
[2,9,0,1,3]

Finally, you can use the deque object from the Python collections module to get rid of the first element in a list.

from collections import deque

list = [1,2,9,0,1,3]

dequelist = deque(list)

dequelist.popleft()

print(list(dequelist))

#Output:
[2,9,0,1,3]

In Python, lists are one of the most used data structures and allow us to work with collections of data easily. When working with lists, it is useful to be able to add or remove items from the list in an easy way.

With Python, we can easily remove the first element from a list. There are multiple ways to remove the first item, and the easiest way to get rid of the first item of a list is with slicing.

To use slicing to remove the first element in a list, we start the slice at position ‘1’ and go to the end by not specifying an end to the slice.

Below is an example of how we can use slicing in Python to get rid of the first element of a list.

list = [1,2,9,0,1,3]

list_without_first_element = list[1:]

print(list_without_first_element)

#Output:
[2,9,0,1,3]

If you are looking to remove the last element from a list, you can also use slicing, or any of these methods which you will see below.

Using the pop Function in Python to Remove the First Item from a List

There are many list methods which allow us to manipulate lists in Python very easily. One such method is the Python pop() function.

The Python pop() function removes an element at the specified position. So, if we want to remove the first element, we pass ‘0’ to pop().

Below is an example of how to use the pop() function to remove the first element from a list in Python.

list = [1,2,9,0,1,3]

print(list.pop(0))

#Output:
[2,9,0,1,3]

Using the del Keyword in Python to Remove the First Element from a List

The Python del keyword allows us to delete objects. We can use the del to delete the first element of a list.

Below is an example in Python of how to use del to delete the first element of a list.

list = [1,2,9,0,1,3]

del list[0]

print(list)

#Output:
[2,9,0,1,3]

Using the remove Function in Python to Remove the First Element from a List

Another list method which is very useful to use in Python is the remove() function. The Python remove() function searches a list for a given element and removes the first match.

We can use the remove() function to get rid of the first item of a list by passing the first item to remove(). Since it’s the first element, we don’t need to worry about duplicates.

Below is an example in Python of how to use remove() to get rid of the first item of a list.

list = [1,2,9,0,1,3]

list.remove(list[0])

print(list)

#Output:
[2,9,0,1,3]

Using deque in Python to Remove the First Item from a List

The last way that you can remove the first item from a list is with the deque data structure from the Python collections module.

Deque, or doubly ended queue, is most useful if you need to quickly append or pop items from the beginning or end of your data. If you have a large collection of items, you deque can be faster than the similar list operations.

To remove the first item from a list using deque, we convert the list to deque, use the popleft() function, and then convert the result back to a list.

from collections import deque

list = [1,2,9,0,1,3]

dequelist = deque(list)

dequelist.popleft()

print(list(dequelist))

#Output:
[2,9,0,1,3]

Hopefully this article has been beneficial for you to learn the different ways you can remove the first element from a list in Python.

Categorized in:

Python,

Last Update: March 20, 2024