lambda, map and filter in Python


lambda operator or lambda function is used for creating small, one-time and anonymous function objects in Python.

Basic syntax

lambda arguments : expression

lambda operator can have any number of arguments, but it can have only one expression. It cannot contain any statements and it returns a function object which can be assigned to any variable.


Function in python:

Above function name is add, it expects two arguments x and y and returns their sum.

Let’s see how we can convert the above function into a lambda function:

In lambda x, y: x + y; x and y are arguments to the function and x + y is the expression which gets executed and its values is returned as output.

lambda x, y: x + y returns a function object which can be assigned to any variable, in this case function object is assigned to the add variable.

If we check type of add, it is a function.

Mostly lambda functions are passed as parameters to a function which expects a function objects as parameter like map, reduce, filter functions


Basic syntax

map(function_object, iterable1, iterable2,...)

map functions expects a function object and any number of iterables like list, dictionary, etc. It executes the function_object for each element in the sequence and returns a list of the elements modified by the function object.


In the above example, map executes multiply2 function for each element in the list i.e. 1, 2, 3, 4 and returns [2, 4, 6, 8]

Let’s see how we can write the above code using map and lambda.

Just one line of code and that’s it.

Iterating over a dictionary using map and lambda

In the above example, each dict of dict_a will be passed as parameter to the lambda function. Result of lambda function expression for each dict will be given as output.

Multiple iterables to the map function

We can pass multiple sequences to the map functions as shown below:

Here, each i^th element of list_a and list_b will be passed as argument to the lambda function.

In Python3, map function returns an iterator or map object which gets lazily evaluated. Just like zip function is lazily evaluated. Lazily evaluation is explained in more detail in the zip function article

Neither we can access the elements of the map object with index nor we can use len() to find the length of the map object

We can force convert the map output i.e. the map object to list as shown below:


Basic syntax

filter(function_object, iterable)

filter function expects two arguments, function_object and an iterable. function_object returns a boolean value. function_object is called for each element of the iterable and filter returns only those element for which the function_object returns true.

Like map function, filter function also returns a list of element. Unlike map function filter function can only have one iterable as input.


Even number using filter function

Filter list of dicts

Similar to map, filter function in Python3 returns a filter object or the iterator which gets lazily evaluated. Neither we can access the elements of the filter object with index nor we can use len() to find the length of the filter object.

  1. Zip in Python
  2. decorators in Python
  3. Concatenating two lists in Python
  4. List comprehensions in Python