Python: Remove first element from a list (5 Ways)

This article will discuss different ways to delete the first element from a list in Python.


Table of Contents

Remove the first element from a list in Python using the pop() function

In Python, the list class provides a function pop(index); it accepts an optional argument index and deletes the element at the given index. Let’s use this function to remove the last element from a list,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

# Remove first element from list in python
list_of_num.pop(0)

print(list_of_num)

Output:

[52, 53, 54, 55, 56, 57, 58, 59]

As we provided the 0 as the index argument in the pop() function, it deleted the first item of the list.

Remove the first element from a list in Python using slicing

We can slice the list to remove the first element. To slice a list, provide the start and end index in the subscript operator. For example,

list[start, end]

It will select the elements from index positions start to end-1. If the start index is not provided, it selects from the first element of list and if end index is not provided, it selects until the end of the list. If the list has N elements, then slice the list to select elements from index position 1 to N. So, to delete the first element from a list, select the elements from 1 to end. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

# Remove first element from list in python
list_of_num = list_of_num[1:]

print(list_of_num)

Output:

[52, 53, 54, 55, 56, 57, 58, 59]

It deleted the first element from the list.

Remove the first element from a list in Python using del keyword

To delete the last element from a list, select the first element from the list and give it to the del keyword to delete it. For example,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

# Remove first element from list in python
del list_of_num[0]

print(list_of_num)

Output:

[52, 53, 54, 55, 56, 57, 58, 59]

It deleted the first item from the list.

Remove the first element from a list in Python using the remove() function

In Python, the list class provides a function remove(value) to delete the first occurrence of a given value from the list. We can use this to delete the first element of the list. For this, select the first element from the list and pass it to the remove() function,

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

# Remove first element from list in python
list_of_num.remove(list_of_num[0])

print(list_of_num)

Output:

[52, 53, 54, 55, 56, 57, 58, 59]

It deleted the first element from the list.

Remove the first element from the list in Python using deque

Convert the given list to deque and pop an element from left. Then cast the remaining elements in deque to the list. For example,

from collections import deque

list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59]

# Remove first element from list in python
queue = deque(list_of_num)
queue.popleft()
list_of_num = list(queue)

print(list_of_num)

Output:

[52, 53, 54, 55, 56, 57, 58, 59]

It deleted the first element from the list.

Summary

We learned about different ways to delete the first element from a list in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top