Python filter list/iterable examples

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.
python-filter

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]
Env: Python 2.7.6

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]
Env: Python 2.7.6

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]
Env: Python 2.7.6

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]
Env: Python 2.7.6

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 0x7f00561be9d0>
[1, 2, 3, 4]
Env: Python 2.7.6

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments