The filter function in Python

The function filter filters an iterable by removing items that don’t match a predicate (a function that returns a Boolean).

Example:

nums = [11, 22, 33]
res = list(filter(lambda x: x%2==0, nums))
print(res)

The terminal output will be:

ddn_ro@linux:~/Desktop$ python file.py
[22]
ddn_ro@linux:~/Desktop$

Note:
Like map, the result has to be explicitly converted to a list if you want to print it.

Leave a Reply