If you want to learn about functions in Python, you can visit the Python course.
In Python, we can also have functions without any name, i.e. anonymous functions, with the use of lambda. You can say that lambda is a tool to create anonymous functions in Python. The syntax to create a function using lambda is:
lambda arguments: expression
For example, we can create a function as:
lambda a: a+2
Here, we have only one argument i.e., 'a' to our function and the expression is 'a+2'. This function will return the value passed after adding 2 to it.
- There are also a few points you must know before using lambda:
- There can be any number of arguments to a function created using lambda. For example, we can also write “lambda a, b: a+b”. Here, we have passed two arguments – a and b.
- But there must be only one expression, which means that we can't have multiple expressions in our lambda function as we have in a regular Python function.
- Also, the result of this expression is returned from the function itself. So, we don't need to explicitly return a value from the function. For example, lambda a: a+2 will return the value of a+2.
- In the place of expression, there should not be something which doesn't return anything. For example, assignments don’t return anything. So, you should not use them in lambda.
Before discussing more about the lambda, let's write a few functions using lambda.
s = lambda a: a+2 print (s(2)) print (s(4)) m = lambda a: a*2 print (m(2)) print (m(4))
Now, you know how to create a lambda function. So, let's discuss the use of lambda function and where to use it.
Use of lambda function
Generally, we make functions when a block of code is repeated several times and that block is too lengthy to write repeatedly. But with the use of lambda, we can only make a function which has a single expression in it. So, what is the use of this function?
There are many situations where we are bound to use functions. For example, think of a function taking another function as its parameter. And its functionality depends upon the function that is being passed to it. So, it might be possible that we need to pass different functions every time for the desired result, and we now need to make all of these function. Here, we can use lambda to pass to the function whenever required, even with different definitions. For example,
foo(lambda x: (x%2==0), some_value)
foo(lambda x: (x%3==0), some_value)
Here, the function 'foo' required a function to be passed into it and the functionality of this function will be different if the function passed returns different values. So instead of making two different functions, we just made two anonymous functions where they were required and kept our code clean and clear. Hopefully, you have got an idea of where lambda can be useful. We will deal with a working example in the next section, filter.
‘filter’ is a built-in function in Python which is used to filter an iterable element. The iterable element can be any container which supports iteration, or an iterator. We will use list in our example. The filter function takes two arguments, one is a function and second is iterable. So, the form is -
filter(function, iterable). The filter function will filter out every element of the iterable for which the function passed to it is false. And we are left with only elements for which the function is true. For example, on a list of numbers from 1 to 10 i.e., [1,2,3,4,5,6,7,8,9,10], we can use the filter function to get the even numbers. We will pass a function which returns
True if the number is even and the list to the filter function. Let's see.
#array from 1 to 10 a = range(1,11) def even(n): if n%2==0: return True return False print (list(filter(even,a)))
We can also use the lambda function instead of creating a regular function to pass to the filter function. This will help keep our code clean. The code for the same is given below.
#array from 1 to 10 a = range(1,11) print (list(filter(lambda x:x%2==0,a)))
[2, 4, 6, 8, 10]
Let's see one more example of filter function.
#array from 1 to 10 a = range(1,11) print (list(filter(lambda x:x%3==0,a)))
[3, 6, 9]
You can see that we are passing different functions every time to get the desired results. Imagine a case to call filter every time without using lambda and creating different functions every time. You can see that lambda helped make the code clearer. This is exactly what was explained in the above section - use of lambda.
‘map’ is also similar to the filter function. It also takes a function and iterable(s). We can pass multiple iterables to the map function. Its syntax is:
map(function, iterable, ...)
‘map’ function implements the function passed to it to every element of the iterable. For example, if the function passed is:
def multiply(n): return 2*n
Then every element of the array will be multiplied by 2. In the case of multiple iterables, the passed function must take those many arguments to it. For example, if we have passed two iterables ‘a’ (a = [1,2,3]) and ‘b’ (b = [4,5,6]), and a function to add two arguments passed to it:
a = [1,2,3] b = [4,5,6] def ad(x, y): return x+y
Then each element of the list ‘a’ will get added to the corresponding element of the list ‘b’. For example, 1 will be added to 4, 2 will be added to 5 and 3 will get added to 6, thus giving us [5,7,9]. Let's see an example of map:
#array from 1 to 10 a = range(1,11) print (list(map(lambda x:x*2,a)))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Example of map with multiple iterables
#array from 1 to 10 a = range(1,11) b = range(11,21) print (list(map(lambda x, y:x+y,a, b)))
[12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
So, we have arrived at the end of this lesson. Hope you were able to get a clear understanding of some important elements in Python and filter out any lingering doubts you had in your mind’s map regarding these concepts.