Remove elements from a List based on a condition in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Remove elements from List based on a condition in Python
  2. Remove elements from List based on a condition using filter()
  3. Remove elements from List based on a condition using for loop
  4. Remove elements from List based on multiple conditions

# Remove elements from list based on a condition in Python

Use a list comprehension to remove elements from a list based on a condition.

The list comprehension will return a new list that doesn't contain any of the elements that don't meet the condition.

main.py
my_list = [22, 55, 99, 105, 155, 205] new_list = [item for item in my_list if item > 100] print(new_list) # ๐Ÿ‘‰๏ธ [105, 155, 205]

remove elements from list based on condition

The code for this article is available on GitHub

We used a list comprehension to remove elements from a list based on a condition.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
main.py
my_list = [22, 55, 99, 105, 155, 205] new_list = [item for item in my_list if item > 100] print(new_list) # ๐Ÿ‘‰๏ธ [105, 155, 205]

On each iteration, we check if the current list item is greater than 100 and return the result.

The new list will only contain values that are greater than 100.

The list comprehension doesn't change the original list, it creates a new list.

# Removing elements from the original list

If you need to change the original list, use a list slice.

main.py
my_list = [22, 55, 99, 105, 155, 205] my_list[:] = [item for item in my_list if item > 100] print(my_list) # ๐Ÿ‘‰๏ธ [105, 155, 205]

removing elements from the original list

The code for this article is available on GitHub

We used the my_list[:] syntax to get a slice that represents the entire list, so we can assign to the variable directly.

The slice my_list[:] represents the entire list, so when we use it on the left-hand side, we are assigning to the entire list.

This approach changes the contents of the original list.

Alternatively, you can use the filter() function.

# Remove elements from list based on a condition using filter()

This is a three-step process:

  1. Use the filter() function to filter out elements that don't meet the condition.
  2. Use the list() class to convert the filter object to a list.
  3. The new list won't contain any elements that don't meet the condition.
main.py
my_list = [22, 55, 99, 105, 155, 205] new_list = list( filter(lambda item: item > 100, my_list) ) print(new_list) # ๐Ÿ‘‰๏ธ [105, 155, 205]

remove elements from list based on condition using filter

The code for this article is available on GitHub

We used the filter() function to remove elements from a list based on a condition.

The filter() function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

The lambda function gets called with each element in the list, checks if the value is greater than 100 and returns the result.

The filter object only contains values that are greater than 100.

The last step is to use the list() class to convert the filter object to a list.

Alternatively, you can use a for loop.

# Remove elements from list based on a condition using for loop

This is a three-step process:

  1. Use a for loop to iterate over a copy of the list.
  2. On each iteration, check if the current item meets a condition.
  3. Use the list.remove() method to remove the matching elements.
main.py
my_list = [22, 55, 99, 105, 155, 205] for item in my_list.copy(): if item <= 100: my_list.remove(item) print(my_list) # ๐Ÿ‘‰๏ธ [105, 155, 205]

remove elements from list based on condition using for loop

The code for this article is available on GitHub
On each iteration, we check if the current item is less than or equal to 100 and use the list.remove() method to remove the matching elements.

The list.remove() method removes the first item from the list whose value is equal to the passed-in argument.

The remove() method mutates the original list and returns None.

The most important thing to note when removing items from a list in a for loop is to use the list.copy() method to iterate over a copy of the list.

If you try to iterate over the original list and remove items from it, you might run into difficult-to-locate bugs.

# Remove elements from list based on multiple conditions

If you need to remove elements from a list based on multiple conditions, use the boolean AND and boolean OR operators.

main.py
my_list = [22, 55, 99, 105, 155, 205] new_list = [ item for item in my_list if item > 100 and item < 200 ] print(new_list) # ๐Ÿ‘‰๏ธ [105, 155]

remove elements from list based on multiple conditions

The code for this article is available on GitHub

The code sample uses the boolean AND operator to check for 2 conditions.

When the boolean AND operator is used, both conditions have to be met for the element to get included in the new list.

Only elements that are greater than 100 and less than 200 get added to the list in the example.

If you need to check for multiple conditions where only 1 has to be met, use the boolean OR operator.

main.py
my_list = [22, 55, 99, 105, 155, 205] new_list = [ item for item in my_list if item > 100 or item == 22 ] print(new_list) # ๐Ÿ‘‰๏ธ [22, 105, 155, 205]
The code for this article is available on GitHub

When the boolean OR operator is used, either condition has to be met for the element to get added to the new list

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev