Python filter()

Python filter() is a powerful function for extracting specific elements from an iterable based on a condition. It works well with lambda functions, None, and other Python functions like map() and sorted(). Mastering Python filter(), you can write cleaner and more efficient code for data filtering tasks.
Table of Contents

Understanding Python filter() Function

Python filter() is a built-in function that allows you to construct an iterator from elements of an iterable for which a function returns true. It filters out items from a sequence based on a given condition. The filter() function takes two arguments: a function that defines the filtering condition and an iterable (like a list, tuple, or string) containing the elements you want to filter. Python filter() applies the function to each item in the iterable, and if the function returns True for that item, the item is included in the result; otherwise, it’s excluded.

Syntax of Python filter()

filtered_iterator = filter(function, iterable)

Explanation

  • filtered_iterator: Variable will store the iterator returned by the filter() function.
  • filter(): Built-in function that filters elements based on the given function.
  • function: Function that tests each element of the iterable; if function returns True, the element is kept, otherwise it’s filtered out. You can use None for this argument as well.
  • iterable: Sequence (e.g., list, tuple, string) you want to filter. It is the second argument passed to filter().

Example of Python filter()

numbers = [1, 2, 3, 4, 5, 6]

def is_even(number):
    return number % 2 == 0

even_numbers = filter(is_even, numbers)
print(list(even_numbers))

Explanation

  • numbers = [1, 2, 3, 4, 5, 6]: Creates a list named numbers.
  • def is_even(number):: Defines a function is_even that checks if a number is even.
  • return number % 2 == 0: Returns True if the number is even, False otherwise.
  • even_numbers = filter(is_even, numbers): Uses filter() to apply is_even to each element of numbers, storing the result in even_numbers.
  • print(list(even_numbers)): Converts the even_numbers iterator to a list and prints it.

Output

[2, 4, 6]


filter() Parameters

Python filter() function takes two parameters: a function and an iterable. The function is applied to each element of the iterable to determine whether it should be included in the result. This function should return True to keep an element and False to filter it out. The iterable can be any sequence, such as a list, tuple, or string, whose elements you want to filter based on the condition specified by the function.

Syntax

filtered_iterator = filter(function, iterable)

Example

scores = [70, 85, 60, 92, 78, 55]

def is_passing(score):
    return score >= 70

passing_scores = filter(is_passing, scores)
print(list(passing_scores))

Explanation

  • scores = [70, 85, 60, 92, 78, 55]: Creates a list scores containing numbers.
  • def is_passing(score):: Defines a function is_passing that checks if a score is 70 or above.
  • return score >= 70: Returns True if the score is passing, False otherwise.
  • passing_scores = filter(is_passing, scores): Uses filter() to apply is_passing to each element of scores, keeping only the passing scores.
  • print(list(passing_scores)): Converts the result to a list and prints it.

Output

[70, 85, 92, 78]


filter() Return Type

Python filter() function returns an iterator, not a list. This iterator yields the items from the original iterable for which the given function returns True. It’s important to understand that filter() doesn’t immediately compute all the results; instead, it creates an iterator that produces the filtered items individually as you request. To get the filtered results as a list, you must explicitly convert the iterator using list().

Example

numbers = [1, 2, 3, 4, 5, 6, 7, 8]

def is_odd(n):
  return n % 2 != 0

odd_numbers_iterator = filter(is_odd, numbers)
print(odd_numbers_iterator)
print(list(odd_numbers_iterator))

Explanation

  • numbers = [1, 2, 3, 4, 5, 6, 7, 8]: Creates a list of numbers.
  • def is_odd(n):: Defines a function is_odd to check if a number is odd.
  • return n % 2 != 0: Returns True if n is odd, False otherwise.
  • odd_numbers_iterator = filter(is_odd, numbers): Applies filter() to get an iterator of odd numbers.
  • print(odd_numbers_iterator): Prints the iterator object (not the filtered elements).
  • print(list(odd_numbers_iterator)): Converts the iterator to a list and prints the filtered elements.

Output

<filter object at 0x…>
[1, 3, 5, 7]


Filter Vowels from List

You can use Python filter() to filter out vowels from a list of characters. To do this, you first define a function that checks if a given character is a vowel. Then, you pass this function along with your list to filter(). The filter() function will apply your vowel-checking function to each character in the list and return an iterator that yields only the characters for which the function returned True (i.e., the vowels).

Syntax

filtered_iterator = filter(is_vowel, list_of_characters)

Explanation

  • filtered_iterator: Variable will store the iterator containing the filtered characters (vowels in this case).
  • filter(): Function applies is_vowel to each element in list_of_characters.
  • is_vowel: Function that returns True if a character is a vowel, False otherwise. It is used as first parameter in filter().
  • list_of_characters: List containing characters to be filtered. It is used as second parameter in filter().

Example

characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

def is_vowel(char):
    vowels = "aeiouAEIOU"
    return char in vowels

vowels_iterator = filter(is_vowel, characters)
print(list(vowels_iterator))

Explanation

  • characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']: Creates a list of characters.
  • def is_vowel(char):: Defines a function is_vowel to check if a character is a vowel.
  • vowels = "aeiouAEIOU": Defines a string containing all vowels.
  • return char in vowels: Returns True if char is in vowels, False otherwise.
  • vowels_iterator = filter(is_vowel, characters): Applies filter() to get an iterator of vowels.
  • print(list(vowels_iterator)): Converts the iterator to a list and prints the vowels.

Output

[‘a’, ‘e’, ‘i’]


Python filter() with Lambda

You can use Python filter() with lambda functions for more concise code. A lambda function is a small, anonymous function defined using the lambda keyword. Instead of specifying a separate function to pass to filter(), you can create a lambda function directly within the filter() call. This is especially handy for simple filtering conditions that don’t require a full function definition.

Syntax

filtered_iterator = filter(lambda x: condition, iterable)

Explanation

  • filtered_iterator: Variable will store the iterator returned by filter().
  • filter(): Function filters the iterable based on the condition in the lambda function.
  • lambda x:: Defines an anonymous function (lambda) that takes one argument x.
  • condition: Condition that’s checked for each element x. It is used inside the lambda function.
  • iterable: Sequence being filtered. It is passed as second parameter to filter().

Example

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers))

Explanation

  • numbers = [1, 2, 3, 4, 5, 6]: Creates a list named numbers.
  • even_numbers = filter(lambda x: x % 2 == 0, numbers): Uses filter() with a lambda function to keep only even numbers.
  • print(list(even_numbers)): Converts the result to a list and prints it.

Output

[2, 4, 6]


Using None as a Function Inside filter()

When you use None as the first Python filter() argument, it acts as an identity function. This means that it will filter out any elements from the iterable that are considered “falsy” in Python (e.g., False, None, 0, empty string, empty list, etc.). Only the elements that evaluate to True will be kept in the result. Using None will remove all the false values from the iterable.

Syntax

filtered_iterator = filter(None, iterable)

Explanation

  • filtered_iterator: Variable will store the iterator returned by filter().
  • filter(): Function filters out falsy elements when the first argument is None.
  • None: Indicates that the identity function should be used (keep only truthy elements). It is used as first parameter in filter().
  • iterable: Sequence being filtered. It is used as second parameter in filter().

Example

values = [0, 1, [], 'hello', False, True, {}, None]
truthy_values = filter(None, values)
print(list(truthy_values))

Explanation

  • values = [0, 1, [], 'hello', False, True, {}, None]: Creates a mixed list with truthy and falsy values.
  • truthy_values = filter(None, values): Uses filter() with None to keep only truthy elements.
  • print(list(truthy_values)): Converts the result to a list and prints it.

Output

[1, ‘hello’, True]


Filtering a Dictionary

While Python filter() is most commonly used with lists and other sequences, you can also filter a dictionary. However, when you apply filter() directly to a dictionary, iterates over the keys, not the key-value pairs. To filter a dictionary by its values, you can use a lambda function that accesses the values through the keys and then provides the filtering condition within the lambda function.

Syntax

filtered_iterator = filter(lambda k: condition_on_dict[k], dictionary)

Explanation

  • filtered_iterator: Variable will store the iterator returned by filter().
  • filter(): Function filters the dictionary based on a condition applied to its values.
  • lambda k:: Defines an anonymous function that takes a key k as input.
  • condition_on_dict[k]: Condition applied to each value in the dictionary. It uses k to get each value using dict[key].
  • dictionary: Dictionary you want to filter. It is passed as second parameter to filter().

Example

my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
filtered_keys = filter(lambda k: my_dict[k] > 2, my_dict)
print(list(filtered_keys))

Explanation

  • my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}: Creates a dictionary.
  • filtered_keys = filter(lambda k: my_dict[k] > 2, my_dict): Filters keys of my_dict where the value is greater than 2.
  • print(list(filtered_keys)): Converts the result to a list and prints the filtered keys.

Output

[‘c’, ‘d’]


Combining filter() with Other Functions

You can combine Python filter() with other functions like map() to create more complex data processing pipelines. For example, you might first use filter() to select elements that meet a specific condition and then use map() to transform those elements. This allows you to chain operations in a functional programming style, making your code concise and readable.

Syntax

result = map(function2, filter(function1, iterable))

Explanation

  • result: Variable will store the final result of the combined operations.
  • map(): Function applies function2 to each element produced by filter().
  • function2: Function is applied to the elements that passed the filter. It is used as first parameter for map().
  • filter(): Function filters elements from iterable based on function1.
  • function1: Function determines which elements to keep. It is used as first parameter for filter().
  • iterable: Original sequence. It is used as second parameter for filter().

Example

numbers = [1, 2, 3, 4, 5, 6]
def is_even(n):
  return n%2 == 0
even_numbers = filter(is_even, numbers)
squared_numbers = map(lambda x: x**2, even_numbers)
print(list(squared_numbers))

Explanation

  • numbers = [1, 2, 3, 4, 5, 6]: Creates a list of numbers.
  • def is_even(n):: Defines a function to check if a number is even.
  • return n%2 == 0: Returns True if number is even, else False.
  • even_numbers = filter(is_even, numbers): Filters numbers to keep only even numbers.
  • squared_numbers = map(lambda x: x**2, even_numbers): Applies a lambda function to square each even number.
  • print(list(squared_numbers)): Converts the result to a list and prints the squared even numbers.

Output

[4, 16, 36]


Conclusion

Python filter() is a function for selectively extracting elements from an iterable based on a given condition. It’s commonly used with a function that defines the filtering logic and returns an iterator that yields the elements that satisfy the condition. Combining filter() with different functions like map() enables you to create elegant and efficient data.


Also Read

Python staticmethod()

Python eval()


Python Reference

python filter()

Table of Contents