In this Python tutorial, we will see how to delete an element from a Python list in different ways provided by Python using demonstrative examples
Lists are used to store multiple items in a single variable. Lists are created using square brackets[]. They are mutable, which means we can change their contents – add, modify, or delete elements.
states = ['California', 'Texas', 'Florida', 'New York', 'Pennsylvania']
What if we don’t want the state ‘Pennsylvania’ on the list? So, let’s know how to delete this element in the Python list.
Deleting an element from a Python list
There are five different ways to delete an element from a Python list. Let’s see them one by one using examples:
- remove()
- del
- pop()
- filter()
- List comprehension
Method-1: Deleting an element using the remove() in a Python list
The remove() method in Python list, removes the specified element using the element name. This comes in use when we don’t care about the index number of the elements in Python list.
For example, let’s take a Python list of famous American rock bands and try delete ‘Nirvana’ from this list. we can use the remove() method:
rock_bands = ['Nirvana', 'Aerosmith', 'Metallica', 'Pearl Jam', 'Foo Fighters']
rock_bands.remove('Nirvana')
print(rock_bands)
The output will be:
['Aerosmith', 'Metallica', 'Pearl Jam', 'Foo Fighters']
This way we can the remove() method to delete an element in a Python list.
Method-2: Deleting an element using the del keyword in a Python list
The del keyword is used to delete an element at the specified index location from the list, but the removed item is not returned. We can also delete a subpart of a Python list
Consider a Python list of American car brands: In the first, we will just delete one element i.e., the element stored at the 2nd index number.
In the second we will use the result list of the 1st one, and delete a subpart of that Python list using list slicing.
car_brands = ['Chevrolet', 'Ford', 'Tesla', 'Dodge', 'Jeep', 'Buick', 'Chrysler']
# this will remove 'Tesla' and the index will change.
del car_brands[2]
print(car_brands)
# this will remove 'Chevrolet', 'Ford' and 'Dodge'.
del car_brands[0:3]
print(car_brands)
The Output is:
['Chevrolet', 'Ford', 'Dodge', 'Jeep', 'Buick', 'Chrysler']
['Jeep', 'Buick', 'Chrysler']
This way we can use del to delete an element or a part of a Python list.
Method-3: Deleting an element using the pop() method in a Python list
The pop() method is also useful to delete an element in a Python list. It removes and returns the item at a given position, or the last item if the index is not specified.
For example, let’s look at a Python list of the top five national parks in the U.S. by visitation: In the first, we will just delete one element i.e., the element stored at the 2nd index number.
In the second we will use the result list of the 1st one, and delete the last element of that Python list because nothing is passed to the pop() method.
national_parks = ['Great Smoky Mountains',
'Yellowstone',
'Zion',
'Rocky Mountain',
'Yosemite']
# This will delete the element having 2 as index number
national_parks.pop(2)
print(national_parks)
# This will delete the last element of the list
national_parks.pop()
print(national_parks)
The output is:
['Great Smoky Mountains', 'Yellowstone', 'Rocky Mountain', 'Yosemite']
['Great Smoky Mountains', 'Yellowstone', 'Rocky Mountain']
This way we can use the pop() method to delete an element either by Index number or last element in Python List.
Method-4: Deleting an element using the filter() function in a Python list
Python’s filter() function creates a list of elements for which a function returns true. This is a handy method when we want to filter out certain elements of a Python list.
Now, let’s consider a Python list of the most popular American dog breeds and we want to remove all breeds whose names are longer than 15 characters. We can do this using the filter() function with lambda function:
Note: The space between the names is also considered as a character.
dog_breeds = ['Labrador Retriever',
'French Bulldog',
'German Shepherd',
'Golden Retriever',
'Bulldog']
dog_breeds = list(filter(lambda breed: len(breed) <= 15, dog_breeds))
print(dog_breeds)
The output is:
['French Bulldog', 'German Shepherd', 'Bulldog']
This way we can eliminate elements in a Python list using the filter() function.
Method-5: Deleting an element using List comprehension in a Python list
List comprehension is a Pythonic way to handle lists. It offers a shorter syntax when we want to create a new list based on the values of an existing Python list.
For example, imagine a Python list that contains names of states in the U.S.A. whose motto is in English We want to remove all the states that don’t start with ‘I’. We can use list comprehension to accomplish this:
states = ['Indiana', 'Oregon', 'Vermont', 'Wisconsin', 'California', 'Illinois']
states_after = [state for state in states if not state.startswith('I')]
print(states_after)
The Output is:
['Oregon', 'Vermont', 'Wisconsin', 'California']
This way we can eliminate elements using List comprehension in a Python list.
Conclusion:
In conclusion, deleting elements from a Python list can be accomplished in several ways. Depending on whether we know the exact position and, value of the item, or if we want to delete elements based on certain criteria, Python provides the remove() method, del keyword, pop() method, filter() function, and list comprehension.
You may also like to read:
- Python List remove() method [With Examples]
- How to remove the first element from a list in Python
- How to remove the last element from 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.