Python lists are one of the most used sequential data structures when it comes to storing and manipulating data. It simplifies the life of programmers to a great extent. In this article, we will take a look at different ways that you can use to remove the first element from the Python list.
But before that, let us take a brief look at the List data structures.
What are Lists in Python?
A list is a Python data structure that can be used to represent a collection of data elements to organize data while programming. They are ordered and mutable. This means that you can add new items to the list, remove items one by one, and rearrange items according to the order in which they appear.
Listing elements with commas between them inside the square brackets([]) is a convenient way to organize data while using a list data structure. Here is a simple example:
sample_list = ["favtutor", 1, 2.30] print(sample_list)
Output:
['favtutor', 1, 2.3]
Also, you should learn how to print a list in Python using different ways.
How to Remove the First Element from a List in Python?
When it comes to removing items from lists, there are multiple ways in which this can be done. Removing elements is not just a matter of removing a value from a list, but also of making sure that the other elements in the list remain valid.
Below are the 6 common methods by which you can remove the first element of any given list in Python:
1) Using pop()
The pop() method is one of the list object method used to remove an element from the list and returns it. While using the pop() method, you have to specify the index value of the element in the list as an argument and return the popped-out element as the desired output.
As we wish to remove the first element of the list here, we pass “0” as the parameter inside the method. If the particular element to be removed is not present in the list, then the error “IndexError” will be raised.
Check out the below example to understand the workings of the pop() method and remove the desired element from the list.
sample_list = [1, 2, 3, 4, 5] a = sample_list.pop(0) print(sample_list)
Output:
[2, 3, 4, 5]
2) Using remove() method
The remove () method is also a common method used to remove and return the list element. Unlike the pop() method, when you make use of the remove() method, you have to specify the particular element to be removed as a method parameter.
If the element is repeated inside the list, then the first occurrence of the element will be removed. At the same time, if the element is not found in the list, then the error “ValueError” will be returned.
The below example shows the working of the remove() method in detail:
sample_list = [1, 2, 3, 4, 5] sample_list.remove(sample_list[0]) print(sample_list)
Output:
[2, 3, 4, 5]
3) Using del operator
The 'del' operator has a similar working as pop() method to remove the elements from the lists. It removes the element from the list at a specified index location but does not return the removed item, unlike the pop() method. So the operator takes the element to be removed as the argument and deletes it from the given list.
The main advantage of this element is that it supports removing more than one element at a time from the list. The program will return the “IndexError” as output when the index of the element is out of range. Here is an example:
sample_list = [1, 2, 3, 4, 5] del sample_list[0] print(sample_list)
Output:
[2, 3, 4, 5]
4) Using slicing method
Slicing is one of the techniques used in Python programming to manipulate strings and lists as data structures. We can use the slicing technique to slice the list from the second element and separate it from the first element. Later, you can return this element as the output as shown in the below example.
Remember that this method does not in-place the removal of elements like the above methods. Check the example below:
sample_list = [1, 2, 3, 4, 5] sample_list = sample_list[1:] print(sample_list)
Output:
[2, 3, 4, 5]
5) Using deque() method
This is the least used method to remove the elements from the given list. In this method, we convert the list into the deque and then use the popleft() method which helps to return the first element from the front of the list.
Remember that to implement this method, you have to import the Python deque at the beginning of the program using the “import” keyword. Check out the below example to understand the working of deque in detail. Here is an example:
from collections import deque sample_list = [1,2,3,4,5] queue = deque(sample_list) queue.popleft() sample_list = list(queue) print(sample_list)
Output:
[2, 3, 4, 5]
6) Using List Comprehension
Python's list comprehension feature makes list creation quick and easy. We can make a list depending on a condition using this strong tool. This section will cover using list comprehension to eliminate the first instance of an element from a list.
Let's think about the following illustration:
fruits = ['apple', 'banana', 'cherry', 'apple', 'orange'] new_fruits = [i for i in fruits if i != 'apple' or fruits.remove('apple')] print(new_fruits)
Output:
['banana', 'cherry', 'orange']
The word "apple" appears twice in the list of fruits in the aforementioned example. The first instance of the word "apple" is then eliminated from the list using list comprehension.
You should also learn how to remove duplicates from a Python list as well.
Conclusion
There are always times when you wish to add, update, or remove certain specified elements from the list. Updating these changes manually after completing the entire program is feasibly impossible. Therefore, we have mentioned the 5 most common and popular methods to remove the first element from the list in Python.
Also, learn how to remove Last Element from List in Python.