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']

Updated on: 16-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements