In this article, we will go through all the methods to remove elements from a list in Python.
Python lists are the most basic data structure used in day-to-day programming. We come across situations where we need to remove elements from lists and in this article, we’ll discuss exactly that.
1. Remove Elements From a List Based on the Values
One of the reasons Python is a renowned programming language is the presence of the numerous inbuilt functions. These inbuilt functions are very handy and thereby make Python very convenient to write.
remove() function
Python has an inbuilt function, remove()
that helps us to remove elements based on the value.
# List of integers
lis = [3, 1, 4, 1, 5, 9, 2, 6, 5]
# Remove element with value = 1
lis.remove(1)
# Printing the list
print(lis)
# Remove element with value = 9
lis.remove(9)
# Printing the list
print(lis)
OUTPUT:
[3, 4, 1, 5, 9, 2, 6, 5]
[3, 4, 1, 5, 2, 6, 5]
The key things to note here are:
- The
remove()
function takes in one argument — the value to be removed. - If there are multiple occurrences of the given value, the first one is removed.
- Removing an element does not leave a blank space at that position, it just shifts the following elements to the left.
- In case, there is no such element in the list, then the script raises an error.
Error-free usage of remove() function
There is an easy way to bypass the error while removing an element in case the programmer is unaware of its presence on the list. We’ll do this using the if condition.
# List of integers
lis = [1, 4, 2, 6, 1, 9, 10]
# Value to be removed
val = 6
# Check if the list contains the value
if val in lis:
# Remove the value from the list
lis.remove(val)
# Printing the list
print(lis)
OUTPUT:
[3, 1, 4, 1, 5, 9, 2, 5]
In the above code snippet, we first check the presence of the value in the list before removing.
Remove all the occurrences of a value in a list
As we previously mentioned, remove() function only removes the first occurrence of a value. In order to take out all the instances of said value, we will do use the while loop.
# List of integers
lis = [1, 4, 2, 6, 1, 9, 10]
# Value to be removed
val = 1
# Run until the list containes the value
while val in lis:
# Remove the value from the list
lis.remove(val)
# Printing the list
print(lis)
OUTPUT:
[3, 4, 5, 9, 2, 6, 5]
This sums up the usage of remove()
function.
2. Removing elements based on an index
There can be a few ways to remove elements based on the index. Let us quickly go through each one of them.
del keyword
del
is a powerful tool in Python which is used to remove entire objects. It can also be used to remove elements from a given list.
# List of integers
lis = [3, 1, 4, 1, 5, 9, 2, 6, 5]
# Removing element from the start (index = 0)
del lis[0]
# Printing the list
print(lis)
# Removing element from the last (index = -1)
del lis[-1]
# Printing the list
print(lis)
OUTPUT:
[1, 4, 1, 5, 9, 2, 6, 5]
[1, 4, 1, 5, 9, 2, 6]
Some of the observations derived from the above script are:
del
is not a method. It is a statement that deletes the item placed after it.- Removing an element from a specific index shifts the next value to that specific index if it is not the last index.
- Providing an index more than (or equal to) the length of the list will raise an “out of range” error.
pop() function
As the name suggests, pop()
function pops out an element from a specified index.
# List of integers
lis = [3, 1, 4, 1, 5, 9, 2, 6, 5]
# Removing the fourth element (index = 3)
lis.pop(3)
# Printing the list
print(lis)
# Removing the second last element (index = -2)
lis.pop(-2)
# Printing the list
print(lis)
OUTPUT:
[3, 1, 4, 5, 9, 2, 6, 5]
[3, 1, 4, 5, 9, 2, 5]
What we learnt about pop()
method here is:
- It takes a single argument — the index of a list
- It removes the element from the list based on the given index. The following elements shift to the left.
- It supports backward indexing.
- It will raise an “out of range” error if the index is not present for the list.
We have a complete article on the use of pop() method.
3. Removing a range of elements from a list
Python has a provision of removing a range of elements from a list. This can be done by del
statement.
# List of integers
lis = [3, 1, 4, 1, 5, 9, 2, 6, 5]
# Removing first and second element
del lis[0:2]
# Printing the list
print(lis)
# Removing last two elements
del lis[-2:]
# Printing the list
print(lis)
OUTPUT:
[4, 1, 5, 9, 2, 6, 5]
[4, 1, 5, 9, 2]
Let us try to understand the process:
- To remove multiple elements from a list in a sequence, we need to provide a range of elements to the
del
statement. - A range of elements take a starting index and/or an ending index, separated by a colon
':'
. - The values to be deleted include starting index, but not the value at the ending index.
- In case, the ending index is missing, the range includes all the elements till the end of the list.
4. Remove all the elements from the list
Python provides a method to empty the entire list in a single line.
# List of integers
lis = [3, 1, 4, 1, 5, 9, 2, 6, 5]
# Removing all elements
lis.clear()
# Printing the list
print(lis)
OUTPUT:
[ ]
If the function applied to an empty list, it does not raise any error.
Conclusion
It is up to your discretion to use the way of removing elements from a list, either by value or index. Different circumstances require a different approach, therefore Python provides various methods of removing elements from a Python list.
We hope the reader had no difficulty in following the article. Feel free to comment below for any queries related to the subject.