Remove first element from list in Python
Last Updated :
11 Feb, 2025
The task of removing the first element from a list in Python involves modifying the original list by either deleting, popping, or slicing the first element. Each method provides a different approach to achieving this. For example, given a list a = [1, 2, 3, 4], removing the first element results in [2, 3, 4].
Using del keyword
del statement is a direct way to remove the first element from a list. It modifies the original list in place, making it a simple and efficient approach for removing elements without creating a new list.
Python
a = [1, 2, 3, 4]
del a[0]
print(a)
Explanation: del a[0] removes the first element, shifting the remaining elements left and updating the list in place.
Using pop()
pop(0) method removes and returns the first element of the list. This is useful when we need to capture the removed element while modifying the list.
Python
a = [1, 2, 3, 4]
a.pop(0)
print(a)
Explanation: a.pop(0) removes and returns the first element of the list, shifting the remaining elements left. The list is updated in place and the removed element is no longer part of the list.
Using slicing
Slicing creates a new list starting from the second element, effectively removing the first element. It's a concise way to remove the first element, especially when we don't mind creating a new list.
Python
a = [1, 2, 3, 4]
a = a[1:]
print(a)
Explanation: slicing a[1:] creates a new list starting from the second element, effectively removing the first element and assigning the result back to a.
Using collections.deque
deque from the collections module is designed for efficient removal of elements from both ends of a list. If we frequently need to remove the first element, this is the best option as it optimizes such operations.
Python
from collections import deque
a = deque([1, 2, 3, 4])
a.popleft()
print(a)
Explanation: a.popleft() removes and returns the first element from the deque, efficiently updating the deque in place by shifting the remaining elements to the left.
Similar Reads
Remove Last Element from List in Python Given a list, the task is to remove the last element present in the list. For Example, given a input list [1, 2, 3, 4, 5] then output should be [1, 2, 3, 4]. Different methods to remove last element from a list in python are:Using pop() methodUsing Slicing TechniqueUsing del OperatorUsing Unpacking
2 min read
Remove falsy values from a list in Python Removing falsy values from a list filters out unwanted values like None, False, 0 and empty strings, leaving only truthy values. It's useful for data cleaning and validation.Using List ComprehensionList comprehension is a fast and efficient way to remove falsy values from a list . It filters out val
1 min read
Remove Key from Dictionary List - Python We are given a list of dictionaries and our task is to remove a specific key from each dictionary in the list. For example, if we have the following list: li = [{'Gfg': 1, 'id': 2, 'best': 8}, {'Gfg': 4, 'id': 4, 'best': 10}, {'Gfg': 4, 'id': 8, 'best': 11}] and the key to remove is "id" then the re
3 min read
How to Remove Item from a List in Python Lists in Python have various built-in methods to remove items such as remove, pop, del and clear methods. Removing elements from a list can be done in various ways depending on whether we want to remove based on the value of the element or index.The simplest way to remove an element from a list by i
3 min read
Remove all the occurrences of an element from a list in Python The task is to perform the operation of removing all the occurrences of a given item/element present in a list. Example Input1: 1 1 2 3 4 5 1 2 1 Output1: 2 3 4 5 2 Explanation : The input list is [1, 1, 2, 3, 4, 5, 1, 2] and the item to be removed is 1. After removing the item, the output list is [
4 min read