Top 3 Python Features You Did Not Know About (Probably)

Hello, Habr! I present to you the translation of the article “Top 3 Python Functions You Don't Know About (Probably)” by Dario Radečić.

Being one of the most popular languages ​​of the 21st century, Python certainly has many interesting features that are worth exploring in detail. Three of them will be considered today, each theoretically, and then with practical examples.

image

The main reason I want to talk about these functions is because they help to avoid writing loops . Cycles can be expensive in some cases, and in addition, these features will help increase speed.

Here are the functions that will be discussed in the article:

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

Even if you have already heard about these functions, there is nothing wrong with reinforcing your knowledge with a little more theory and examples.

So, without further ado ... Let's get started!

map ()


The map () function takes as a parameter another function along with some array. The idea is to apply a function (passed as an argument) to each element in the array.

This comes in handy for two reasons:

  1. You do not need to write a loop
  2. It's faster than a loop

Let's look at it in action. I will declare a function “num_func ()” that takes a single number as a parameter. This number is squared, divisible by 2, and returned as such. Note that the operations were chosen arbitrarily, you can do anything inside the function:

image

Now let's declare an array of numbers to which we want to apply “num_func ()”. Note that “map ()” itself will return the display object , so you need to convert it to a list:

image

It looks like the process completed successfully. There is nothing revolutionary here, but it would be nice to avoid cycles when possible.

filter ()


Here is another decent feature that will save you time - both in writing and in execution. As the name implies, the idea is to store in the array only those elements that satisfy a certain condition .

As in the case of “map ()”, we can declare a function in advance, and then pass it to “filter ()” along with an iterable object (for example, a list).

Let's look at it in action. I went further and declared a function “more_than_15 ()”, which, as the name implies, returns “true” if the element specified as a parameter is greater than 15:

image

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

image

As expected, only three values ​​satisfy this condition. Once again, nothing revolutionary here, but it looks much better than the cycle.

reduce ()


Now "reduce ()". It is slightly different from the previous two. First we need to import it from the functools module. The main idea is that it will apply this function to an array of elements and as a result will return a single value .

The last part is crucial - “reduce ()” does not return an array of elements, it always returns a single value. Let's take a look at the diagram to concretize this concept:

image

Here is the logic written in case the diagram is not 100% clear:

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

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

image

Now we can return to the diagram in the code and make sure everything works as it should:

image

Until you go to the comments section, I know perfectly that there are other ways to summarize list items. This is the simplest example to demonstrate how a function works.

Before you leave:


I hope that you can somehow use these three functions in your daily life. The speed increase may not be significant - it depends on the amount of data you work with - but the code will look better with fewer loops.

If you have other examples, feel free to share them in the comments section.

Thanks for reading.

All Articles