The world’s leading publication for data science, AI, and ML professionals.

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

Cleaner Code and Fewer Loops? Count me in.

As one of the most popular languages of the 21st century, Python certainly has a lot of interesting functions worth exploring and studying in-depth. Three of those will be covered today, each theoretically and through practical examples.

Photo by Max Duzij on Unsplash
Photo by Max Duzij on Unsplash

The main reason why I want to cover these functions is that they help you avoid writing loops. Loops can be expensive to run in some cases, and alongside that, these functions will help with speed improvement.

Here are the functions that the article will cover:

  1. map()
  2. filter()
  3. reduce()

Even if you’ve heard of those functions before there’s no harm in reinforcing your knowledge with a bit more theory and examples.

So without further ado, let’s get started!


map()

The map() function takes in another function as a parameter, alongside an array of some sort. The idea is to apply a function (the one passed in as an argument) to every item in the array.

This comes in handy for two reasons:

  1. You don’t have to write a loop
  2. It’s faster than a loop

Let’s see it in action. I will declare a function called num_func() that takes one number as a parameter. That number is squared and divided by 2 and returned as such. Note that the operations were chosen arbitrarily, you can do anything you want inside the function:

And now let’s declare an array of numbers on which we want to apply num_func(). Note that map() itself will return a map object so you’ll need to convert it to a list:

Seems like the process finished successfully. There’s nothing groundbreaking here, but it’s a good thing to avoid loops whenever possible.


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.


reduce()

Now reduce() is a bit different than the previous two. To start out, we have to import it from the functools module. The main idea behind this is that it will apply a given function to the array of items and will return a single value as a result.

The last part is crucial – reduce() won’t return an array of items, it always returns a single value. Let’s see a diagram to make this concept concrete.

Here’s the logic written out in case diagram isn’t 100% clear:

  1. 5 gets added to 10, results in 15
  2. 15 gets added to 12, results in 27
  3. 27 gets added to 18, results in 45
  4. 45 gets added to 25, results in 70

And 70 is the value that gets returned. To start out with the code implementation, let’s import reduce function from functools module and declare a function that returns a sum of two numbers:

Now we can revisit the diagram in code, and verify that everything works as it should:

Don’t jump into the comment section just yet – I’m perfectly aware that there are other ways to sum items of a list. This is just the most simple example to show how the function works.


Before you go

I hope you can somehow utilize these three functions in your daily routine. The speed improvement might not be drastic – depends on the volume of data you are dealing with – but the code will generally look nicer with fewer loops.

If you have some other examples, don’t hesitate to share it in the comment section.

Thanks for reading.


Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.

Join Medium with my referral link – Dario Radečić


Related Articles