Python Filter, Map and Reduce Functions


The default functions in Python make it easier for us to write code, as well as speed up the work done. When writing code, The most important thing we need is to process values in a list, change some of them, and filter some of them. The map, filter and reduce concepts exist to make this work much faster. Then let me start with the examples.;


It is a great function that we can use instead of returning for to do any of the operations on the list. Taking 2 parameters, operation, the variable to which the operation will be performed. When doing a transaction, the more we add Python’s default function structure to the transaction, the more optimized it is, so we have fast-running code. Let me put it an example;


We have a list of 10000 data, we need to multiply this list by 2. We will do this in 3 different ways,

The standard method for
using lambda and map function
using map function but using method

When we run this code, we observe that the result with lambda and map is 2 times more efficient than for, and we see that the result is between these two in making it with the help of the method.;



As you can imagine, filter is used to get the appropriate ones in any sequence. The function takes 2 variables for this operation, the first is the desired condition, and the second is the list where the operation will be performed. This is an example of the process again, let us observe through it.;


We have a list of the same length again, assuming that the number of pairs in this list;

Lets check;

Rather than being close to each other, another beauty here is the importance of the operation done with a few lines of code. We had to define the number of lines and many variables in one line while processing one line.


This is a function that we can use when we want to obtain a final value from the data in a list. Again, taking 2 variables, the first variable to be done in the process, 2. we also give the list to be processed in value. Let us say this with an example;


If we have a list of 10,000 data, let’s find the sum of the numbers in this list.;

Lets Check;

It is interesting to note that the reduce function is slow, which is because it has a 2-way collection by writing extra methods in lambda and keeping it in 3 variables. I hope it will be more performance in more complex transactions.