Top 3 Python Functions You Don’t Know About (Probably)

Original article can be found here (source): Artificial Intelligence on Medium

filter()

Here’s another one decent function that will save you time — both on writing and on execution. As the name suggests the idea is to keep in array only the items that satisfy a certain condition.

Just like with map(), we can declare the function beforehand, and then pass it to filter() alongside the list of iterables.

Let’s see this in action. I’ve gone ahead and declared a function called more_than_15(), which, as the name suggests, will return True if item given as parameter is greater than 15:

Next, we declare an array of numbers and pass them as a second parameter in the filter() function:

As expected, only three values satisfy the given condition. Once again, nothing groundbreaking here, but looks a lot better than a loop.