Lambda, Map, and Filter in Python
A look at the syntax and usage of each function
Today’s piece covers using lambda, map, and filter functions in Python. We’ll be covering the basic syntax of each and walking through some examples to familiarize yourself with using them. Let’s get started!
lambda operator or
lambda function is used for creating small, one-time, anonymous function objects in Python.
lambda arguments : expression
lambda operator can have any number of arguments but can have only one expression. It cannot contain any statements and returns a function object which can be assigned to any variable.
Let’s look at a function in Python:
The above function’s name is
add, it expects two arguments
y and returns their sum.
Let’s see how we can convert the above function into a
lambda x, y: x + y;
y are arguments to the function and
x + y is the expression that gets executed and its values are returned as output.
lambda x, y: x + y returns a function object which can be assigned to any variable, in this case, the function object is assigned to the
If we check the type of
add, it is a
lambda functions are passed as parameters to functions that expect function object as parameters such as
map(function_object, iterable1, iterable2,...)
map functions expect a function object and any number of
iterables, such as
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 the
multiply2 function for each element in the list,
[1, 2, 3, 4], and returns
[2, 4, 6, 8].
Let’s see how we can write the above code using
Just one line of code!
Iterating Over a Dictionary Using Map and Lambda
In the above example, each
dict_a will be passed as a parameter to the
lambda function. The result of the
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:
i^th element of
list_b will be passed as an argument to the
In Python3, the
map function returns an
map object which gets lazily evaluated, similar to how the
zip function is evaluated. Lazy evaluation is explained in more detail in the
zip function article.
We can’t access the elements of the
map object with index nor we can use
len() to find the length of the
We can, however, force convert the
map output, i.e. the
map object, to
list as shown below:
filter function expects two arguments:
function_object and an iterable.
function_object returns a boolean value and is called for each element of the
filter returns only those elements for which the
map function, the
filter function also returns a list of elements. Unlike
filter function can only have one
iterable as input.
Even number using
filter function in Python3 returns a
filter object or the iterator which gets lazily evaluated. We cannot access the elements of the
filter object with index, nor can we use
len() to find the length of the f