# Why Use the filter() Function?

In this lesson, you’ll see why you’d want to use the `filter()` function rather than, for example, a for loop with an if statement. You’d get the same result, but functional programming allows you to chain function calls. This allows you to avoid side effects and have a line of code that gives you a quick snapshot of what is happening.

When doing functional programming, you have a bunch of functions as your basic building blocks, and then you can use and reuse them together in different contexts.

matt7

You give some very good arguments why the filter style is good in this case. It would help if you could also have a quick discussion on the performance and memory usage impacts of this style of using filter vs. loops/if.

Lipsa

where can i find the notebook for this series?

Ricky White RP Team

Hi Lipsa. We don’t have notebooks for our series. Sorry.

sandeepranjan

I am getting an error when using the filter function:

``````mathS = tuple(filter(lambda x : x.field == 'math', scientists))
pprint(mathS)
``````

Throws below error. I do have the necessary imports and rest of the code is identical to what is being shown in this tutorial -

Any Idea what am I missing ?

``````Traceback (most recent call last):
File "/Users/sandeep/Library/Preferences/PyCharmCE2019.3/scratches/scratch.py", line 23, in <module>
mathS = tuple(filter(lambda x : x.field == 'math', scientists))
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/fnmatch.py", line 51, in filter
pat = os.path.normcase(pat)
File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/posixpath.py", line 54, in normcase
s = os.fspath(s)
TypeError: expected str, bytes or os.PathLike object, not tuple
``````

@sandeepranjan: It’s odd that you’re seeing an exception somewhere in the `os` module (`posixpath.py` and `fnmatch.py` in the Traceback)… I don’t see anything wrong with your `filter` expression upon first inspection.

I’m wondering if this is an issue with your local Python environment, maybe you could try your program on a different machine or inside a Docker container with a clean CPython install to see if the error goes away.

jaman

Hi Dan, thanks for the great videos. I want to understand your thought process about when/if data should be stored in a pandas dataframe instead of storing data in basic types like tuples, lists, dictionaries, etc. My impression is that data analysis/viz/modeling work uses pandas, but I’m wondering what you consider while deciding if data should belong in a pandas df.

IMO the main benefits of using Pandas (which is built on top of NumPy) over a homegrown solution are:

2. Performance benefits due to vectorization

Nanogines

What editor do you use?

Rashikraj Shrestha

``````# we can reuse filter function with a reusable function
def nobel_filter(x):
return x.nobel is True
``````
``````tuple(filter(lambda x: nobel_filter, Scientists))
``````
``````(Scientist(name='Ada Lovelace', field='math', born=1815, nobel=False),
Scientist(name='Emmy Noether', field='math', born=1882, nobel=False),
Scientist(name='Marie Curie', field='math', born=1867, nobel=True),
Scientist(name='Tu Youyou', field='physics', born=1930, nobel=True),
Scientist(name='Vera Rubin', field='chemistry', born=1928, nobel=False),
Scientist(name='Sally Ride', field='physics', born=1951, nobel=False))
``````

what is the issue over here?? i could not exploit it

Sagar Rathod

Hi @Rashikraj Shrestha, You should have called that function nobel_filter(x) inside lambda insted of just nobel_filter.

Thanks Dan, for Nice Explanation.

Rashikraj Shrestha

@Sagar Rathod, but in this tutorial it is clearly instructing to use nobel_filter only

Victor R Cardoso

@Rashikaj, you added the extra lambda expression:

``````tuple(filter(lambda x: nobel_filter, Scientists))
``````

This function will return everything as True, given that for each case `lambda x` will return the function `nobel_filter` (without calling it!).

You’ll need to remove the lambda to use the function:

``````tuple(filter(nobel_filter, Scientists))
``````

In this case it will call the function for each case.

Otherwise, if you want to keep the lambda you’ll need to make it calling the `x` case explicitly:

``````tuple(filter(lambda x: nobel_filter(x), Scientists))
``````

I have not checked, but this should work fine.

Thank you for the discussion.

Yes that’s correct, good explanation Victor :)

Nikhil Omkar

Does functional programming reduce space and time complexities?

Bartosz Zaczyński RP Team

@Nikhil Omkar That’s an interesting question!

In terms of space, I’d argue that FP is more likely to be memory demanding compared to other programming paradigms. While FP was invented in the ’50s, it wasn’t adopted until recently because of the high memory cost at the time. On the one hand, functions can share immutable data, but even the slightest mutation results in making lots of copies. Moreover, all those function calls need to be stored on a stack before they can be processed. Unless the language comes with a few smart tricks like tail call optimization, the memory consumption will most likely be higher than in a non-functional code.

Regarding speed, another reason why FP is getting noticed these days are multi-core CPUs. The time for Moore’s law has long gone. CPU vendors have been investing in stuffing more cores onto a chip rather than making the individual cores faster. This means programmers need to learn to take advantage of parallel computing, and functional programming helps with that.

Anyway, the choice of a particular programming paradigm will be much less important from the time and space complexity perspective than the algorithm’s choice.

Jon David

I <3 lambdas

to join the conversation.

Lesson Completed!
Lesson Bookmarked
Request Failed :(