
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do you remove multiple items from a list in Python?
To remove more than one item from a list, we can use various ways as discussed in this article. Let’s say have the following input List −
["David","Jacob", "Harry", "Mark", "Anthony", "Steve", "Chris"]
Following is the output when multiple elements “David” and “Harry” are removed −
["Jacob", "Mark", "Anthony", "Steve", "Chris"]
Remove multiple items from a List
Example
To remove multiple items from a List, use the del keyword. The del allows you to add the items you want to delete in a range using square brackets:
# Creating a List mylist = ["David","Jacob", "Harry", "Mark", "Anthony", "Steve", "Chris"] # Displaying the List print("List = ",mylist) # Remove multiple items from a list using del keyword del mylist[2:5] # Display the updated list print("Updated List = ",list(mylist))
Output
List = ['David', 'Jacob', 'Harry', 'Mark', 'Anthony', 'Steve', 'Chris'] Updated List = ['David', 'Jacob', 'Steve', 'Chris']
Remove multiple items from a list using List Comprehension
Example
To remove multiple items from a List, we can also list comprehension. In this, first we will set the elements to be deleted and then add it to the List Comprehension for deleting them −
# Creating a List mylist = ["David","Jacob", "Harry", "Mark", "Anthony", "Steve", "Chris"] # Displaying the List print("List = ",mylist) # Remove the following multiple items delItems = {"David","Anthony","Chris"} # List Comprehension to delete multiple items resList = [i for i in mylist if i not in delItems] # Display the updated list print("Updated List = ",resList)
Output
List = ['David', 'Jacob', 'Harry', 'Mark', 'Anthony', 'Steve', 'Chris'] Updated List = ['Jacob', 'Harry', 'Mark', 'Steve']
Remove multiple items from a list using remove()
Example
In this example, we will remove multiple items from the List. The items getting removed are divisible by 5 −
# Creating a List mylist = [2, 7, 10, 14, 20, 25, 33, 38, 43] # Displaying the List print("List = ",mylist) # Delete multiple items (divisible by 5) for i in list(mylist): if i % 5 == 0: mylist.remove(i) # Display the updated list print("Updated List = ",mylist)
Output
List = [2, 7, 10, 14, 20, 25, 33, 38, 43] Updated List = [2, 7, 14, 33, 38, 43]
Remove multiple items from a list with index
Example
Here, we will be given with the index of the items to be removed −
# Creating a List mylist = ["David","Jacob", "Harry", "Mark", "Anthony", "Steve", "Chris"] # Displaying the List print("List = ",mylist) # Remove the items at the following indexes delItemsIndex = [1, 4] # List Comprehension to delete multiple items for i in sorted(delItemsIndex, reverse = True): del mylist[i] # Display the updated list print("Updated List = ",mylist)
Output
List = ['David', 'Jacob', 'Harry', 'Mark', 'Anthony', 'Steve', 'Chris'] Updated List = ['David', 'Harry', 'Mark', 'Steve', 'Chris']
- Related Articles
- How do you remove duplicates from a list in Python?
- How do I remove multiple elements from a list in Java?
- How to remove items from a list in C#?
- How do I copy items from list to list without foreach in C#?
- How do you create a list from a set in Java?
- Python Program to remove items from set
- How to remove index list from another list in python?
- How to remove an object from a list in Python?
- How to remove an element from a list in Python?
- How to remove multiple selected items in the listbox in Tkinter?
- Python Program to remove items from the set
- Python | Remove empty tuples from a list
- How do you split a list into evenly sized chunks in Python?
- How do you copy a list in Java?
- How do you create a list in Java?

Advertisements