How to use python built-in function filter with example

neotam Avatar

How to use python built-in function filter with example
Posted on :
,

Python built-in function filter is helpful sometimes to write a compact code. Filter function takes two arguments “function” and “iterable”. Each element of iterator which is returned by iterable is passed to function (first argument). Function must return either True or False or equivalent. All elements are filtered for which function returns True. Signature of filter function

filter(function, iterable) 

Filter function returns the generator which is equivalent to

(i for i in iterable if function(i))

Let’s take a simple example to apply filter .

Q) Write a program that will that will return total number of vowels and total number of unique vowels in a given string as tuple

General approach to write a program for given scenario

msg = "hello! welcome to world of python. It is most elegant programming language "


def countVowels(msg):
    total_vowels = 0
    unique_vowels = set()
    vowels = 'aeiou'
    for i in msg: 
        if i in vowels: 
            total_vowels += 1 
            unique_vowels.add(i)
    return (total_vowels, len(unique_vowels))

print(countVowels(msg))

Same program can be re-written effectively using filter function in very compact way as follows

(lambda i: (len(x:=list(i)), len(set(x))) ) (filter(lambda c: c in 'aeiou', msg))

:= is new operator known as walrus operator introduced in python 3.8. Read more at Python operators reference .

Above both program would return

(21, 5)

If new walrus opeartor := is not used we would get some unintended results since filter returns generator but not list. It is mistake often python beginners tend to make often with generators or iterators

(lambda i: (len(list(i)), len(set(i))) ) (filter(lambda c: c in 'aeiou', msg))
(21, 0)

Expected output is (21, 5) but actual output of above code is (21, 0) because generator is consumed by first len(list(i)) leaving nothing to be consumed by set(i) expression. This is where new walrus operator comes to rescue to assign evaluated value(list) to variable x within expression to refer it later lambda i: (len(x:=list(i)), len(set(x))) .

Counter part of this function which filters elements if function returns False is available in module itertools itertools.filterfalse .

Write a program using itertools.filterfalse for the given scenario as follows

Q) Write a program that will that return total number of consonants and unique consonants in a given string

Simple code for the given scenario using filterfalse is as follows

from itertools import filterfalse 
(lambda i: (len(x:=list(i)), len(set(x))) ) (filterfalse(lambda c: c in 'aeiou', msg))

(54, 18)

Other related built-in functions are

all(), any(), and map()

Leave a Reply

Your email address will not be published. Required fields are marked *