Python filter is very useful built-in python function to achieve complex stuff over a list in very short and elegant way. Note that filter applies to any iterable (e.g. list). We’ll use list for the purpose of this tutorial.
Usage: filter(function, iterable)
The function should take one argument and should return boolean value. If true is returned for a value, that value is taken.
Here are some examples.
filter a list by applying a function
Usage: filter(function, iterable)
Given a list of numbers get a new list with items less than 100.
def is_lt_100(x): return x < 100 a = [1,2,3,4,1000] b = filter(is_lt_100, a) print b
[1, 2, 3, 4]
filter a list by applying a lambda function
Given a list of numbers get a new list with items less than 100 using lambda function.
a = [1,2,3,4,1000] b = filter(lambda x: x < 100, a) print b
[1, 2, 3, 4]
filter a list using [x for x in list if ()]
a = [1,2,3,4,1000] b = [x for x in a if (x < 100)] print b
[1, 2, 3, 4]
filter a list to get non empty value
Filter a list to get non empty values. We can use either None value as function (it will use identity function which will return false for empty value) or bool function.
a = [0,1,0,2] b = filter(None, a) print b c = filter(bool, a) print c
[1, 2] [1, 2]
filter a list and get iterable
This approach will be more memory efficient for large iterable. For big data and ETL operations, we should use this approach.
import itertools a = [1,2,3,4,1000] b = itertools.ifilter(lambda x: x < 100, a) print b print [x for x in b]
<itertools.ifilter object at 0x7f9aff2806d0> [1, 2, 3, 4]