Join my Asyncio(Python) course on Udemy 👀

Filter lists in Python

March 26, 2021  ‐ 3 min read

If you need to filter a list in Python than the built-in filter() function is probably what you're looking for. The filter() function takes a function as its first argument and an iterable as its second argument.

An iterable is basically an object you can iterate through. Like in a for-loop for example.

The filter() function does return a filter object, which is iterable as well. So you can directly use the result in a for loop for example. It isn't a list however, so list slicing on filter results will break your code.

The function that filter() takes as its first argument is used to evaluate whether elements should be present in the filtered result. If you use None instead of a filter function that all elements that evaluate to False are filtered out. As the example below shows.

things = ['', False, 0, None, 5, 'Dog', True]

# Remove Falsy values from list
true_things = list(filter(None, things))

print(true_things)
# => [5, 'Dog', True]

The filter() function keeps the original list intact, it doesn't actually remove values from the list you pass it.

But most often you want to be a little more specific when filtering a list. You can do this by writing your own function that returns True for elements that you want to keep and False for elements that can be filtered out.

numbers = [-2, -1, 0, 1, 2]

def is_positive(number):
    return number > 0

result = filter(is_positive, numbers)

print(list(result))
# => [1, 2]

Filter using a lambda function

Filtering using a lambda function can be a way to achieve the same result more concisely. Lambda functions are actually expressions, therefore you can create them at their point of use. In our case that would be directly as the first argument of the filter() function.

numbers = [-2, -1, 0, 1, 2]

result = filter(lambda n: n > 0, numbers)

print(list(result))
# => [1, 2]

Filter list by dict value

With a custom filter function we can basically filter on anything. Just as such you can filter dictionaries in a list by on of its values.

particles = [
    {
        'name': 'neutron',
        'charge': 0
    },
    {
        'name': 'proton',
        'charge': 1
    },
    {
        'name': 'electron',
        'charge': -1
    },
]

def is_positive(particle):
    return particle['charge'] > 0

result = filter(is_positive, particles)

print(list(result))
# => [{'name': 'proton', 'charge': 1}]

Filter list by object property

The example of the previous section can easily be translated to work with objects too. Where we in this case filter on a property of the object.

class Particle:
    def __init__(self, name, charge):
        self.name = name
        self.charge = charge

    def __repr__(self):
        return f'<Particle: {self.name}>'

particles = [
    Particle('neutron', 0),
    Particle('proton', 1),
    Particle('electron', -1),
]

def is_positive(particle):
    return particle.charge > 0

result = filter(is_positive, particles)

print(list(result))
# => [<Particle: proton>]

Filter with list comprehension

Using list comprehensions is ofter considered to be the more Pythonic way of doing things. And filtering a list using list comprehension works fine too.

Reusing one of the examples above using list comprehension looks as follows.

numbers = [-2, -1, 0, 1, 2]

result = [n for n in numbers if n  > 0]

print(result)

In contrary to the filter() function, list comprehension obviously returns a list. So there is no need to wrap the result in a list().