Remove First Element from List in Python

How to remove the first element/item from a list in Python? To remove the first element from the list, you can use the del keyword or the pop() method with an index of 0. Besides these, you can also use the list.remove(), slicing, list comprehension and deque() + popleft().

In this article, I will explain how to remove the first element from a list by using all these methods with examples.

1. Quick Examples of Removing First Element from List

If you are in a hurry, below are some quick examples of how to remove the first element from a list.


# Quick examples of remove first element from list

# Consider the list of strings
technology = ["Python", "Spark", "Hadoop","Java", "Pandas"]

# Example 1: Remove first element from list 
# Using pop() method
first_element = technology.pop(0)

# Example 2: Remove first element from list 
# Using del Keyword
del technology[0]

# Example 3: Using list.remove() function
technology.remove(technology[0])

# Example 4: Remove first element from list 
# Using slicing
first_element = technology[1:]

# Example 5: Remove first element from list 
# Using list comprehension
first_element = [x for x in technology[1:]]

# Example 6: Remove first element from list 
# Using deque() + popleft()
first_element = deque(technology)
first_element.popleft()

2. Remove First Element from List Using pop() Method

You can remove the first element from a list using the pop() method in Python by passing an index 0 as argument to the pop() method. This will remove the first element from the list and return the element it removed.

Related:


# Initialize the the list with strings
technology = ["Python", "Spark", "Hadoop","Java", "Pandas"]
print("Actual List: ",technology)

# Remove first element from list 
# Using pop() method
first_element = technology.pop(0)
print("Final List: ",technology)
print("Remove first element from list : ",first_element)

In the above example first, create a list named technology containing five elements. Then called the pop() method on technology with an index of 0, which removes the first element ‘Python’ from the list and returns it. Assign the returned value to a variable first_element and print both technology and first_element to verify that the first element has been removed from the list.

Yields below output.

python list remove first element

3. Using del Keyword

You can also remove the first element from a list using the del keyword in Python. Like above use the index 0 to reference the first element of the list and then use del to remove it.


# Consider the list of strings
technology = ["Python", "Spark", "Hadoop","Java", "Pandas"]
print("Actual List: ",technology)

# Remove first element from list 
# Using del Keyword
del technology[0]
print("Final List: ",technology)

Here, first create a list called technology containing five elements. Then used the del keyword to remove the first element from the list by referencing it with the index 0.

Yields below output.

python list remove first element

4. Using list.remove() Function

You can also use a list.remove() to remove an item from list, you can simply pass the value of the first element into this function. It removes the first occurrence of the specified value in the list.


# Consider the list of strings
technology = ["Python", "Spark", "Hadoop","Java", "Pandas"]
print("Actual List: ",technology)

# Using list.remove() function
technology.remove(technology[0])
print("Final List: ",technology)

Yields the same output as above.

5. Remove First Element from List Using Slicing

You can also remove the first element from a list using the slicing technique. Use the slicing technique to select a particular portion of a list. For example, select from 1 to end point of list returns a new list that contains all the elements of the original list except for the first one.

Related: You can select random element from the list.


# Consider the list of strings
technology = ["Python", "Spark", "Hadoop","Java", "Pandas"]
print("Actual List: ",technology)

# Remove first element from list 
# Using slicing
first_element = technology[1:]
print("Final List: ",first_element)

Yields the same output as above.

6. Remove First Element Using List Comprehension

You can also remove the first element from the list using Python list comprehension. The list comprehension [x for x in technology[1:]] is used to create a new list named first_element that contains all the elements of the technology list except for the first one.


# Consider the list of strings
technology = ["Python", "Spark", "Hadoop","Java", "Pandas"]
print("Actual List: ",technology)

# Remove first element from list 
# Using list comprehension
first_element = [x for x in technology[1:]]
print("Final List: ",first_element)

Here, the slicing notation technology[1:] is used to create a new list that starts from the second element of the original list, and the list comprehension iterates over this new list and assigns each element to the variable x. The resulting list is a new list that contains all the elements of the original list except for the first one.

Yields the same output as above.

7. Remove First Element from List Using deque() & popleft()

You can also use the deque() and popleft() functions to remove the first element from the list. deque is a class from the collections module in Python.


from collections import deque
 
# Consider the list of strings
technology = ["Python", "Spark", "Hadoop","Java", "Pandas"]
print("Actual List: ",technology)

# Remove first element from the list 
# Using deque() & popleft()
first_element = deque(technology)
first_element.popleft()
print("Final List: ", list(first_element))

Yields the same output as above.

8. Conclusion

In this article, I have explained how to remove the first element from a list in Python by using the pop(), del Keyword, and list.remove(), slicing, list comprehension and deque() with popleft() with examples.

Happy Learning !!

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply