How to remove multiple items/elements from a list in Python? You can remove multiple items from a list in Python using many ways like, if
control statements, list comprehension, enumerate()
, list slicing, and for loop. In this article, I will explain how to remove multiple items from a list by using all these methods with examples.
1. Quick Examples of Removing Multiple Items from a List
If you are in a hurry, below are some quick examples of how to remove multiple items from a list.
# Quick examples of removing multiple items from a list
# Initialize list
mylist = [10, 5, 8, 9, 12, 7, 4, 15]
# Example 1: Remove multiple items from a list
# Using if…else control statements
for item in mylist:
if (item % 2) != 0:
mylist.remove(item)
# Example 2: Using list comprehension
# To remove all even numbers
mylist = [item for item in mylist if item % 2 != 0]
# Example 3: Remove multiple items from a list
# Using enumerate function
mylist = [item for i, item in enumerate(mylist) if item % 2 != 0]
# Example 4: Remove multiple items from a list
# Using list slicing
del mylist[2:6]
# Example 5: Using a for loop
# To remove multiple items
indexes_to_remove = [0, 3, 6]
for item in sorted(indexes_to_remove, reverse = True):
del mylist[item]
# Example 6: Using list comprehension
remove_set = {5, 9, 15}
mylist = [item for item in mylist if item not in remove_set]
2. Remove Multiple Items From a List Using If Statement
You can remove multiple items from a list using the if control statement. To iterate the list using Python for loop at a specific condition. For every iteration use a if
statement to check the condition, if the condition is true, then remove the items from the list using the remove() method.
In the below example, you can create a list mylist
with some integers. Then you can apply loop through each item mylist
using a copy of the list (i.e., mylist:)
to avoid unexpected behavior due to modifying the list while iterating over it. You use an if statement to check if the item is odd (i.e., not divisible by 2
), and if it is, you remove it from the list using the remove()
method.
# Initialize list
mylist = [10, 5, 8, 9, 12, 7, 4, 15]
print("Original list: ", mylist)
# Remove multiple items from a list
# Using if…else control statements
for item in mylist:
if (item % 2) != 0:
mylist.remove(item)
print("After removing all odd numbers: ", mylist)
Yields below output.
3. Remove Multiple Items From a List Using List Comprehension
You can also use list comprehension to remove multiple elements from a list. Using list comprehension we can create a new list with concise code.
In the below example, you can create a new list from the original list using list comprehension. The expression [item for item in mylist if item % 2 != 0]
iterates over each element/item in mylist
and includes it in the new list only if it is not divisible by 2
(i.e., it is odd). The resulting list contains only the odd elements from the original list.
# Initialize list
mylist = [10, 5, 8, 9, 12, 7, 4, 15]
print("Original list: ", mylist)
# Using list comprehension
# To remove multiple elements
mylist = [item for item in mylist if item % 2 != 0]
print("After removing all even numbers: ", mylist)
Yields the same output as above.
4. Remove Multiple Items From a List Using enumerate()
You can also use list comprehension and the enumerate()
function to remove multiple elements from a list. Enumerate()
function is a built-in function provided by Python. It takes an iterable object such as a list, tuple, set, string, and dictionary and returns enumerate object. This function automatically returns the counter(index) of each value present in the iterable object.
For instance, you can use list comprehension along with enumerate()
function to create a new list containing only the odd elements from the original list. The expression [item for i, item in enumerate(mylist) if item % 2 != 0]
iterates over each element/item mylist
along with its index i
. It includes the elements in the new list only if it is not divisible by 2
(i.e., it is odd). The resulting list contains only the odd elements from the original list.
# Initialize list
mylist = [10, 5, 8, 9, 12, 7, 4, 15]
print("Original list: ", mylist)
# Remove multiple items from a list
# Using enumerate function
mylist = [item for i, item in enumerate(mylist) if tem % 2 != 0]
print("After removing all even numbers: ", mylist)
Yields the same output as above.
5. Remove Multiple Items From a List Using Slicing
You can also use list slicing to remove multiple items from a list. For example, use list slicing to remove values from the index 2
to 5
(i.e., the third to sixth elements) from the given list named mylist
. The expression mylist[2:6]
returns a new list containing the elements at indices 2
to 5
, and you can use the del
statement to remove these elements from mylist
.
# Initialize list
mylist = [10, 5, 8, 9, 12, 7, 4, 15]
print("Original list: ", mylist)
# Remove multiple items from a list
# Using list slicing & del operator
del mylist[2:6]
print("After removing values from index 2 to 5: ", mylist)
# Output:
# Original list: [10, 5, 8, 9, 12, 7, 4, 15]
# After removing values from index 2 to 5: [10, 5, 4, 15]
6. Remove Multiple Items From a List Using For Loop
You can also use a for loop to remove multiple items from a list. For instance, you create a list named indexes_to_remove
containing the indices of the elements, which you want to remove from the list. Then you can use a for loop to iterate over the indices in indexes_to_remove
. For every iteration, to delete the corresponding element of each index in an index_to_ramove
use del
statement.
# Initialize list
mylist = [10, 5, 8, 9, 12, 7, 4, 15]
print("Original list: ", mylist)
# Using a for loop
# To remove multiple items
indexes_to_remove = [0, 3, 6]
for item in sorted(indexes_to_remove, reverse = True):
del mylist[item]
print("After removing values at index 0, 3 and 6: ", mylist)
# Output:
# Original list: [10, 5, 8, 9, 12, 7, 4, 15]
# After removing values at index 0, 3 and 6: [5, 8, 12, 7, 15]
Follow the below example, If the elements to be deleted are known, you can directly remove those elements from the list without caring about their indexes. For example, you create a set named indexes_to_remove
with three integers, 5
, 9
, and 15
. Finally, you can use list comprehension to create a new list that only contains elements from mylist
that are not in the indexes_to_remove
set, and assigns that new list to the variable mylist
.
# Initialize list
mylist = [10, 5, 8, 9, 12, 7, 4, 15]
print("Original list: ", mylist)
# Using list comprehension
remove_set = {5, 9, 15}
mylist = [item for item in mylist if item not in remove_set]
print("After removing multiple numbers: ", mylist)
# Output:
# Original list: [10, 5, 8, 9, 12, 7, 4, 15]
# After removing multiple numbers: [10, 8, 12, 7, 4]
As you can see, the numbers 5
, 9
, and 15
were removed from the original list because they were included in the indexes_to_remove
set.
7. Conclusion
In this article, I have explained how to remove multiple items from a list in Python by using an if
control statement, list comprehension, enumerate()
, list slicing, and for loop with examples.
Happy Learning !!