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
()
filter
()filtered_iterator = filter(function, iterable)
Explanation
filtered_iterator
: Variable will store the iterator returned by thefilter()
function.filter()
: Built-in function that filters elements based on the given function.function
: Function that tests each element of theiterable
; iffunction
returnsTrue
, the element is kept, otherwise it’s filtered out. You can useNone
for this argument as well.iterable
: Sequence (e.g., list, tuple, string) you want to filter. It is the second argument passed tofilter()
.
Example of Python filter
()
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 namednumbers
.def is_even(number):
: Defines a functionis_even
that checks if a number is even.return number % 2 == 0
: ReturnsTrue
if the number is even,False
otherwise.even_numbers = filter(is_even, numbers)
: Usesfilter()
to applyis_even
to each element ofnumbers
, storing the result ineven_numbers
.print(list(even_numbers))
: Converts theeven_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 listscores
containing numbers.def is_passing(score):
: Defines a functionis_passing
that checks if a score is 70 or above.return score >= 70
: ReturnsTrue
if the score is passing,False
otherwise.passing_scores = filter(is_passing, scores)
: Usesfilter()
to applyis_passing
to each element ofscores
, 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 functionis_odd
to check if a number is odd.return n % 2 != 0
: ReturnsTrue
ifn
is odd,False
otherwise.odd_numbers_iterator = filter(is_odd, numbers)
: Appliesfilter()
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 appliesis_vowel
to each element inlist_of_characters
.is_vowel
: Function that returnsTrue
if a character is a vowel,False
otherwise. It is used as first parameter infilter()
.list_of_characters
: List containing characters to be filtered. It is used as second parameter infilter()
.
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 functionis_vowel
to check if a character is a vowel.vowels = "aeiouAEIOU"
: Defines a string containing all vowels.return char in vowels
: ReturnsTrue
ifchar
is invowels
,False
otherwise.vowels_iterator = filter(is_vowel, characters)
: Appliesfilter()
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 byfilter()
.filter()
: Function filters theiterable
based on thecondition
in the lambda function.lambda x:
: Defines an anonymous function (lambda) that takes one argumentx
.condition
: Condition that’s checked for each elementx
. It is used inside the lambda function.iterable
: Sequence being filtered. It is passed as second parameter tofilter()
.
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 namednumbers
.even_numbers = filter(lambda x: x % 2 == 0, numbers)
: Usesfilter()
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 byfilter()
.filter()
: Function filters out falsy elements when the first argument isNone
.None
: Indicates that the identity function should be used (keep only truthy elements). It is used as first parameter infilter()
.iterable
: Sequence being filtered. It is used as second parameter infilter()
.
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)
: Usesfilter()
withNone
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 byfilter()
.filter()
: Function filters the dictionary based on a condition applied to its values.lambda k:
: Defines an anonymous function that takes a keyk
as input.condition_on_dict[k]
: Condition applied to each value in the dictionary. It usesk
to get each value usingdict[key]
.dictionary
: Dictionary you want to filter. It is passed as second parameter tofilter()
.
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 ofmy_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 appliesfunction2
to each element produced byfilter()
.function2
: Function is applied to the elements that passed the filter. It is used as first parameter formap()
.filter()
: Function filters elements fromiterable
based onfunction1
.function1
: Function determines which elements to keep. It is used as first parameter forfilter()
.iterable
: Original sequence. It is used as second parameter forfilter()
.
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
: ReturnsTrue
if number is even, elseFalse
.even_numbers = filter(is_even, numbers)
: Filtersnumbers
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.