How to Remove Multiple Elements from a List in Python [5 Methods]

Do you want to remove multiple elements from a list in Python? In this Python tutorial, I will explain different methods that will help you remove multiple elements from a list in Python using some demonstrative examples.

To remove multiple elements from a list in Python, we can use several methods like list comprehension for filtering elements, the remove() method for specific values, the filter() function to create a new list based on a condition, the del keyword for removing elements by index, and list slicing for selectively keeping elements.

Remove multiple elements from a list in Python Methods

There are five different methods present to remove multiple elements from a list in Python:

  1. List comprehension
  2. remove() function
  3. filter() function
  4. del keyword
  5. List slicing

Let’s see them one by one using some illustrative examples:

1. Python remove multiple items from the list using list comprehension

List comprehension is a concise way to create lists. It’s especially useful for filtering elements out of a list, which means to remove multiple elements from a list in Python. For instance:

states = ["California", "Texas", "Florida", "New York", "Illinois"]
states_to_remove = ["Texas", "Florida"]
new_states = [state for state in states if state not in states_to_remove]
print(new_states)

Output:

['California', 'New York', 'Illinois']

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

remove multiple elements from a list in Python

2. Delete multiple values from a list in Python using the remove() function

The remove() method is used to remove the first occurrence of an element in the list in Python. If we need to remove multiple occurrences, we will have to use a loop in Python. For example:

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
cities_to_remove = ["Houston", "Phoenix"]
for city in cities_to_remove:
    if city in cities:
        cities.remove(city)
print(cities)

Output:

['New York', 'Los Angeles', 'Chicago']

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

Delete multiple elements in a list by index in Python

3. Remove multiple elements from a list in Python using the filter() function

The filter() function constructs an iterator from elements of an iterable for which a function returns True and, hence remove multiple elements from a list in Python. For instance:

landmarks = ["Statue of Liberty", "Grand Canyon", "Yellowstone", "Mount Rushmore"]
landmarks_to_remove = ["Grand Canyon", "Mount Rushmore"]
new_landmarks = list(filter(lambda x: x not in landmarks_to_remove, landmarks))
print(new_landmarks)

Output:

['Statue of Liberty', 'Yellowstone']

After executing the code in Pycharm, one can see the output in the below screenshot.

removing multiple elements from a list in Python

4. How to remove multiple items from a list using the del keyword

The del keyword in Python is used to delete objects. When it comes to lists, we can use del with slicing to remove multiple elements from a list in Python. For example:

universities = ["MIT", "Stanford", "Harvard", "Caltech", "Princeton"]
del universities[1:4:2]
print(universities)

Output:

['MIT', 'Harvard', 'Princeton']

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

How to remove multiple elements from a list of lists in Python

5. Remove multiple elements from a list using list slicing

List slicing can be useful for removing a sequence of elements from a list based on their indices. This method is ideal when we know the positions of the elements to be removed. For example:

novels = ["To Kill a Mockingbird", "The Great Gatsby", "Moby-Dick", "The Catcher in the Rye", "Beloved"]
new_novels = novels[:1] + novels[3:]
print(new_novels)

Output:

['To Kill a Mockingbird', 'The Catcher in the Rye', 'Beloved']

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

Remove multiple items from list in Python

Conclusion

Here, we have learned different ways to remove multiple elements from a list in Python, like using list comprehension, remove(), filter(), del keyword, and slicing. Each method has its own use, helping you to easily and effectively handle different situations when working with lists. Knowing these methods is very useful for writing better and more efficient Python code.

READ:  How to uppercase first letter in a Python list [6 Methods]

You may also like to read: