Functional Programming

Functional Programming: filter() function in Python

Transforming data structures!

Vishal Sharma
The Startup
Published in
3 min readJun 29, 2020

--

Photo by Nathan Dumlao on Unsplash

In my previous article, I talked about functional programming. Along with it, I kept my emphasis on immutable data structures. The gist from the previous article was that immutable data structures form the core of functional programming.

So, in this post, I will take a dataset and transform it using filter() function in Python.

Our dataset consists of GOT characters and I have stored them individually as a namedtuple object and combined them in a tuple. Reason being- no part of the data must be modified.

got_chars=(
got_char("name":"John","profession":"King's Hand","experience_in_year":3),
got_char("name":"Arya Stark","profession":"No One","experience_in_year":4),
got_char("name":"Sansa Stark","profession":"Queen Winterfell","experience_in_year":1),
got_char("name":"Danaerys","profession":"Queen","experience_in_year":3)
)

Working with the filter() function

The filter() function is one of the FP aka functional programming primitives that you can use in your Python code. The documentation defined filter() function as:

“Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.”

filter(function, iterable): Looks like filter() takes a function object and an iterable.

Basically, filter() function allows us to get those data structure elements that pass a certain condition. Let’s say, I want to extract the data of characters who have experience of more than 1 year.

lambda approach

filter(lambda x: x.experience_in_year > 1, got_chars)

For defining a function inline, I used the built-in lambda expression. My lambda expression checks the experience of each character. And, the iterable “got_chars” allow lambda function to do that.

If I run this above line of code, I will get an object id as the output. In fact, it is iterable which carries the characters who have passed the test i.e x.experience_in_year > 1.

foo=filter(lambda x: x.experience_in_year > 1, got_chars)
next(foo)
[Output]: got_char("name":"John","profession":"King's Hand","experience_in_year":3)

It kind of behaves like an iterator as we are accessing all the filtered characters using next().

But, I am looking for something concrete where I can store all the characters in one data structure like a list or a tuple. One way is to simply enclose the filter function inside a tuple or a list.

foo=tuple(filter(lambda x: x.experience_in_year > 1, got_chars))
print(foo)
[Output]: got_char("name":"John","profession":"King's Hand","experience_in_year":3),
got_char("name":"Arya Stark","profession":"No One","experience_in_year":4),
got_char("name":"Danaerys","profession":"Queen","experience_in_year":3)

You can play with filter function in a million ways. You can transform any data using just one line of code.

Pythonic way vs FP way

I could have got to the same answer using a ‘for’ loop. So, why do it this way?

for i in got_chars:
if i.experience_of_years>1:
print(i)

This above line of code would have also landed me at the same output. At one glance, you might say that the ‘for’ loop takes more lines of code. Yes, that’s true!

But, using filter() function is kind of more declarative. It might look confusing because there is loads of stuff going on in just one line of code. But, if you cut it into sets and look at what is happening, the filter() function will make more sense.

Not only that, but it also increases the reusability of the code. Though it is just one line, it can still be tweaked to get other results. And, that’s an amazing thing!

Defining function explicitly

There is another approach to filter() function i.e defining functions explicitly.

def my_fun(row):
if row.experience_of_years>1:
return row
foo=tuple(filter(my_fun,got_chars))
print(foo)

I have defined a function my_fun which does the same thing lambda was doing earlier. But for sake of explicitness, I created a function that is taking a row from the defined dataset and applying a condition to the row for extracting transformed data.

Summary

That’s the end of the filter()! In this post, I mainly talked about filter() function and ease it provides to us while programming. Not to forget, it is one of the functional programming primitives that you can use in your Python code.

In my next articles, I will be focussing on map() and reduce() which are other FP primitives.

Reference

Peace!

--

--