Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do you remove multiple items from a list in Python?
Removing multiple items from a list in Python can be accomplished using several approaches. Each method has its own advantages depending on whether you're removing by value, index, or condition.
Using del with Slice Notation
The del keyword with slice notation removes consecutive items by index range ?
# Creating a List
names = ["David","Jacob", "Harry", "Mark", "Anthony", "Steve", "Chris"]
# Displaying the List
print("List =", names)
# Remove multiple items from a list using del keyword
del names[2:5]
# Display the updated list
print("Updated List =", names)
List = ['David', 'Jacob', 'Harry', 'Mark', 'Anthony', 'Steve', 'Chris'] Updated List = ['David', 'Jacob', 'Steve', 'Chris']
Using List Comprehension
List comprehension creates a new list by filtering out unwanted items ?
# Creating a List
names = ["David","Jacob", "Harry", "Mark", "Anthony", "Steve", "Chris"]
# Displaying the List
print("List =", names)
# Remove the following multiple items
items_to_remove = {"David", "Anthony", "Chris"}
# List Comprehension to delete multiple items
filtered_list = [name for name in names if name not in items_to_remove]
# Display the updated list
print("Updated List =", filtered_list)
List = ['David', 'Jacob', 'Harry', 'Mark', 'Anthony', 'Steve', 'Chris'] Updated List = ['Jacob', 'Harry', 'Mark', 'Steve']
Using remove() with Loop
The remove() method can delete items based on conditions. We iterate over a copy to avoid index shifting issues ?
# Creating a List
numbers = [2, 7, 10, 14, 20, 25, 33, 38, 43]
# Displaying the List
print("List =", numbers)
# Delete multiple items (divisible by 5)
for num in numbers.copy():
if num % 5 == 0:
numbers.remove(num)
# Display the updated list
print("Updated List =", numbers)
List = [2, 7, 10, 14, 20, 25, 33, 38, 43] Updated List = [2, 7, 14, 33, 38, 43]
Removing by Multiple Indices
To remove items at specific indices, sort the indices in reverse order to avoid index shifting ?
# Creating a List
names = ["David","Jacob", "Harry", "Mark", "Anthony", "Steve", "Chris"]
# Displaying the List
print("List =", names)
# Remove the items at the following indexes
indices_to_remove = [1, 4]
# Remove items starting from highest index
for index in sorted(indices_to_remove, reverse=True):
del names[index]
# Display the updated list
print("Updated List =", names)
List = ['David', 'Jacob', 'Harry', 'Mark', 'Anthony', 'Steve', 'Chris'] Updated List = ['David', 'Harry', 'Mark', 'Steve', 'Chris']
Comparison
| Method | Modifies Original? | Best For |
|---|---|---|
del with slice |
Yes | Consecutive items by index |
| List comprehension | No (creates new) | Filtering by value/condition |
remove() loop |
Yes | Items matching conditions |
| Multiple indices | Yes | Specific non-consecutive positions |
Conclusion
Use del with slicing for consecutive items, list comprehension for filtering, and remove() loops for condition-based removal. Always consider whether you need to modify the original list or create a new one.
