This course has been updated! We now recommend you take the Complete Intro to Computer Science course.

Check out a free preview of the full Four Semesters of Computer Science in 5 Hours course:
The "Filter Function" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours course featured in this preview video. Here's what you'd learn in this lesson:

- The filter function takes a list of items and decides which items should stay in the list and which items should be removed by returning either true or false each time it is called. The result is a new list containing only the items that returned true.

Get Unlimited Access Now

Transcript from the "Filter Function" Lesson

>> [MUSIC]

>> Brian Holt: Basically, all filters is gonna do is it's going to be like map, that it's gonna apply something to every item in the list. But, if it returns true, it keeps it in the list and if returns false then it takes out a list. So, you can just filter out a bunch of stuff that you don't need.

[00:00:18] So, let's look at what that looks like.
>> Brian Holt: So if I filter out odds, basically what I'm gonna say is I'm gonna take in a number, and if it's modulus two, that means it's an even number that it's going to return true and that's it's going to stay in the list.

[00:00:39] If it returns false by saying num modulus 2 = 1 right? It's not going to be equal to zero. It's going to return false and this is gonna be taken out of the list right? So looking down here, filter out odds right? Notice that you get back here just even numbers.

[00:00:59] Okay.
>> Brian Holt: So down here, I have a list of just random people. And what we're gonna do out here, is we're gonna filter by state. So it's gonna take in a list, and it's gonna take in a state. And if a person state ends up being this in, this terms just like California, Washington whatever.

>> Brian Holt: Then it's gonna filter out those people. So in this particular case, filter state. If I filter by only people in California, then it's only in return people that have California. And is gonna filter out everyone from Utah, Oregon, Texas, whatever.
>> Brian Holt: Okay, and then my filter, or this is basically what filter looks like on the inside.

[00:01:52] And basically it says, if you return false then I'm not gonna do this if block. But if it returns true, then I'm gonna push you into the list.
>> Brian Holt: Any questions about that about filter? So hopefully you can see that just with map filter reduce, which I'm gonna say about, I don't know, I'm gonna say it more than half of your functional programming is an application of one of those three functions.

[00:02:19] You can chain those together and get these running transformations that just Transform your data into one thing into exactly what you're looking for. And so what you'll do is just take one transformation at a time and just keep applying it. It's very readable. It also tends to not be super performant as you can imagine, right?

[00:02:35] Cuz you're calling a lot of functions. Usually it doesn't matter. Usually performance is not the bottleneck in this particular case. Usually refactoring and having crappy code is a lot bigger bottleneck for you, like your team as a whole. So usually what I do is I'll start out with a functional programming style and if that becomes not performant later in the future, then I go back and optimize and make it iterative or something like that so.