In this Python tutorial, we will see how to remove the first element from a list in Python using different methods with practical examples.
In Python, a list is a mutable (changeable) collection of items. Each item, or element, can be of any data type. They are enclosed within square brackets, and each item is separated by a comma. For example:
states = ["California", "Texas", "Florida", "New York", "Illinois"]
Here, we have a Python list of five U.S. states. Sometimes, we may want to remove the first element from this list.
Remove the first element from a list in Python
There are several ways to accomplish this, which we will detail below:
- del statement
- pop() method
- List slicing
- remove() method
- List comprehension
we will see them one by one using demonstrative examples.
Method-1: remove the first element of a Python list using the del statement
One of the most straightforward methods to remove the first element from a Python list is to use the del statement in Python. The del statement deletes an element at a specific index.
Note: In Python, list positive indexing starts from 0.
For example, we have a list of the first five U.S. Presidents: Let’s try to remove the ‘Washington’ from the list.
presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"]
del presidents[0]
print(presidents)
The output is: After this operation, “Washington” is removed from the list.
['Adams', 'Jefferson', 'Madison', 'Monroe']
This way we can use the del statement to remove the first element of the Python list.
Method-2: Remove the first element of the Python list using the pop() method
The pop() method is another way to remove an element from a list in Python. By default, pop() removes and returns the last element from the list. However, we can also specify the index of the element to be removed. So, here we will use the index number of the first element to remove it.
For example, Consider a list of popular American sports:
sports = ["Football", "Basketball", "Baseball", "Hockey", "Soccer"]
removed_sport = sports.pop(0)
print(sports)
The output is: In this case, “Football” is removed from the list.
['Basketball', 'Baseball', 'Hockey', 'Soccer']
This way we can use the pop() method to remove the first element from a Python list.
Method-3: Remove the first element from a Python list using List slicing
List slicing is a method in Python list to extract a part of a list. This technique can also be used to remove the first element from a Python list. The list slicing includes the start point and excludes the stop point.
For example, Let’s take the largest cities in the U.S. by population. So, here we have mentioned the start point for list slicing(it is included) but the stop point is not mentioned so it will go till the last of the list:
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
cities = cities[1:]
print(cities)
The output is: Here as we are using the same variable name for the result. so, the result will overwrite the previously stored data. This will create a new list without “New York” with the same name.
['Los Angeles', 'Chicago', 'Houston', 'Phoenix']
This way we use list slicing to remove the first element of the Python list.
Method-4: Remove the first element of the Python list using the remove() method
The remove() method in Python removes the first occurrence of a specified value from a list. However, we must know the actual value that we want to remove, not just its position. If we want to remove the first element of the Python list.
For example, If we have a list of the Great Lakes:
lakes = ["Superior", "Michigan", "Huron", "Erie", "Ontario"]
lakes.remove("Superior")
print(lakes)
The output is: After this operation, “Superior” is removed from the list.
['Michigan', 'Huron', 'Erie', 'Ontario']
This way we can use the remove() method to remove the first element of the Python list.
Method-5: Remove the first element of the Python List using List comprehension
List comprehension provides a concise way to create or modify lists in Python. It can be utilized to remove the first element of a list. In this, we’ll be creating a new list in Python that includes every element from the list except for the first one.
For example, We have a list of the top five U.S. universities:
universities = ["MIT", "Stanford", "Harvard", "Caltech", "Chicago"]
universities = [universities[i] for i in range(1, len(universities))]
print(universities)
The output is: Here as we are using the same variable name for the result. so, the result will overwrite the previously stored data. This will create a new list without “MIT” with the same name.
['Stanford', 'Harvard', 'Caltech', 'Chicago']
This way we can use the list comprehension to remove the first element of the Python list.
Note: we can use negative index numbers also.
Conclusion:
In conclusion, Removing the first element from a list in Python can be accomplished using several methods, including the del statement, the pop() method, list slicing, the remove() method, and list comprehension. The method we choose to use depends on our specific needs and the context in which we’re working.
You may also like to read:
- How to delete an element from a Python list
- How to return multiple values from a Python list
- How to remove duplicate elements from a List in Python
- How to get the last element in the Python list
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.