Last Updated:

Lambda, map and filter, reduce in Python

Lambda, map and filter in Python

In this article, we will take a closer look at what functional programming is, as well as about some methods (map, lambda, and others). If you are a beginner or you are just interested in this topic, then we begin.

Lambda, map and filter in python

 

Lambda is roughly speaking, a function that can be used once (one-time). It can be used only when creating and it is used, as correctly, in combination with other functions. For example, map, filter, zip.

mile_distances = [1.0, 6.5, 17.4, 2.4, 9]
kilometer_distances = list(map(lambda x: x * 1.6, mile_distances))

print (kilometer_distances)

Map is a very useful function and here's why: it takes in 2 arguments, one of which is a function and the second is a sequence. It will run through the entire sequence and apply the specified function. Of course, you can use loops for such purposes, but it is more profitable to use map, if only because it takes up much fewer lines of code.

old_list = [‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’]
new_list = list(map(int, old_list))
print (new_list)

It is worth saying that the map will work, even if you give it 2 sequences, then it will simply stop working on the one in which there are fewer elements.

Filter - it is not difficult to guess that this function simply sorts some sequence. It, like a map, takes two arguments: a function and a sequence.

list = ['dandelion', 'rice', 'dandelion', 'dandelion', 'rice', 'dandelion', 'rice', 'rice', 'dandelion']
sortedList = list(filter(lambda x: x == 'rice', list))
print ('rice
', 'rice', 'rice', 'rice', 'rice', 'rice', 'rice']

The distinguishing feature of filter is that the function must return True (False) values in order for the entire sorting process to proceed correctly.

Map, filter, reduce

So, we've already seen a few map functions that apply some kind of lambda expression to the entire sequence, filter, which filters the sequence, now let's look at what to reduce. Reduce — Allows you to apply a specific function to a sequence and returns two values. Let's look at an example.

Before we move on to the examples, it is worth saying that in Python 2 reduce came out of the box, and in Python 3 it was removed, so you need to connect manually.

from functools import reduce
number = [3,6,7,9,10]
result = reduce(lambda a,b: a + b, number)
print (result) => 32

Functional programming in python

Functional programming is a programming approach that treats an application as calculating mathematical functions. Usually, speaking of such a method, automatically means the functions that we have already passed today.

Map, zip, and lambda functions in the examples

Let's see how some functions work in practice:

map
OldList= [’12’, ’22’, ’33’, ’45’, ’56’, ’67’, ’78’]
NewList= list(map(int, OldList))
print (NewList)
[12, 22, 33, 45, 56, 67, 78]

As you can see, here map, turned the numbers found in the array as strings into real numbers.

zip
one = [1,2,3]
two = «xyz»
three = (False, None)
result = list(zip(one, two, three))
print (result) => [(1, ‘x’, False), (2, ‘y’, None)]

Zip finishes its execution when the smallest list ends.

lambda
>>> sum = lambda x,y: x + y
>>> sum(25, 17)
42

Why is map and reduce better?

 

These functions have a number of advantages, which we will now touch upon:

  • they reduce the code significantly.
  • map and reduce are functional, that is, unlike loops, they will not affect the code that is before or after them;
  • they make the code more readable;
  • they have many "friends" who make it easier to work with them (find, any, all and filter).

In conclusion, I want to remind you that it is useless to consume knowledge and not use it in practice. So launch your development environment right now and start applying everything you've learned in practice today. Good luck in mastering programming!