Remove elements from list based on length in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove elements from list based on length in Python #

Use a list comprehension to remove elements from a list based on the length, e.g. new_list = [item for item in my_list if len(item) > 2]. The list comprehension will return a new list that doesn't contain any elements that don't meet the condition.

main.py
# ✅ Remove elements from list based on length (list comprehension) my_list = ['a', 'ab', 'abc', 'abcd', 'abcde'] new_list = [item for item in my_list if len(item) > 2] print(new_list) # 👉️ ['abc', 'abcd', 'abcde'] # ----------------------------------------------- # ✅ Remove elements from list based on length (filter()) new_list = list(filter(lambda item: len(item) > 2, my_list)) print(new_list) # 👉️ ['abc', 'abcd', 'abcde'] # ----------------------------------------------- # ✅ Remove elements from list based on length (for loop) for item in my_list.copy(): if len(item) <= 2: my_list.remove(item) print(my_list) # 👉️ ['abc', 'abcd', 'abcde']

The first example uses a list comprehension to remove elements from a list based on the length.

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 = ['a', 'ab', 'abc', 'abcd', 'abcde'] new_list = [item for item in my_list if len(item) > 2] print(new_list) # 👉️ ['abc', 'abcd', 'abcde']

On each iteration, we check if the current item has a length greater than 2 and return the result.

The new list will only contain values that have a length greater than 2.

The len() function returns the length (the number of items) of an object.

The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

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

If you want to remove the elements from the original list, use list slicing.

main.py
my_list = ['a', 'ab', 'abc', 'abcd', 'abcde'] my_list[:] = [item for item in my_list if len(item) > 2] print(my_list) # 👉️ ['abc', 'abcd', 'abcde']

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 length using filter() #

To remove elements from a list based on length:

  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.
main.py
my_list = ['a', 'ab', 'abc', 'abcd', 'abcde'] new_list = list(filter(lambda item: len(item) > 2, my_list)) print(new_list) # 👉️ ['abc', 'abcd', 'abcde']

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

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.

On each iteration, we check if the current item has a length greater than 2 and return the result.

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 length using for loop #

To remove elements from a list based on length:

  1. Use a for loop to iterate over a copy of the list.
  2. Check the length of each element in the list.
  3. Use the list.remove() method to remove the elements that don't meet the condition.
main.py
my_list = ['a', 'ab', 'abc', 'abcd', 'abcde'] for item in my_list.copy(): if len(item) <= 2: my_list.remove(item) print(my_list) # 👉️ ['abc', 'abcd', 'abcde']
On each iteration, we check if the current item has a length less than or equal to 2 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.

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.