How to use Python lambda, map and filter functions

Python lambda function, aka anonymous function. Unlike how we create a function with def ... statement, where we can name a function, the lambda function doesn’t require a name. It’s useful when we need a quick and (generally a small) function which doesn’t need to be re-used often. Using Lambda function on its own probably doesn’t make too much sense. The value of lambda function is where it’s used together with another function such as map() or filter().

Intro to lambda function

A lambda function:

  • Doesn’t require a name
  • Can take any number of arguments
  • returns only 1 expression

Let’s look at an example of a normal def function vs a lambda function. We are going to create a function to calculate the square of a value.

>>> def square(x):
	return x ** 2

>>> square(3)
9


>>> lambda_sq = lambda x: x**2
>>> lambda_sq(3)
9

I know, I know that I said lambda is anonymous function. In the above example, I assigned a name lambda_sq to it. But the lambda function syntax actually doesn’t require a name.

lambda arguments: expression

Intro to map()

A map() function basically runs a specific function on each item in an iterable (such as a list or tuple). For example, let’s calculate the square of numbers from 1-10. We start off by creating a function square, which returns the square of a give number. Then we can created a list from 1-10 using list comprehension. Note the below code output – a is a map object, which is an iterable, and we can convert it into a list by list(a).

>>> def square(x):
	return x ** 2

>>> li = [i for i in range(1,11)]
>>> li
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a = map(square, li)
>>> a
<map object at 0x000001A366423B50>
>>> list(a)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

In this example, we had to pre-define a function that calculates the square of a number. Let’s assume that this square() function is only used once by the map function, then no longer used. In this situation, it’s better to use a lambda function to calculate the square. Below is the same example using a lambda function.

>>> a_2 = map(lambda x: x**2, li)
>>> a_2
<map object at 0x000001A3663B7670>
>>> list(a_2)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Intro to filter()

A filter() function is kind of similar to map(). However, map() execute a specific function on an iterable and returns every element from that iterable. Whereas filter() returns just the elements for which a function returns True. Let’s see this in an example where we have a list of number 1-20, and we want to return only the odd numbers. We’ll start off by creating a list of 1-20 using list comprehension.

>>> li = [i for i in range(1,21)]
>>> li
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Then let’s define a function that checks whether an input is an odd number, the function returns True if the given number is odd.

>>> def is_odd(x):
	return x % 2 != 0

>>> is_odd(2)
False
>>> is_odd(1)
True

Now let’s try it with the map() function first, see what do we get.

>>> a = map(is_odd, li)
>>> list(a)
[True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False]

As expected, the map() function took is_odd(), and apply to every single item (1-20). The returned value is an iterable containing either True or False, which is the values returned by is_odd().

On the other hand, when we replace map() with filter(), this is what we get:

>>> a_2 = filter(is_odd,li)
>>> a_2
<filter object at 0x000001A366423B50>
>>> list(a_2)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Again, this is expected as the filter() function “filtered” on the list and returned the elements where is_odd() returned True.

Now I know lambda, map and filter. What’s next?

You probably already guessed it! Any column (i.e. pandas series) in a pandas dataframe is an iterable, so we can use the same technique on pandas dataframe! Next chapter we’ll talk about how to create some complex calculated columns.