Anonymous/Lambda Functions in Python

Introduction:

Generally, in python, we get names for functions, but anonymous functions are defined without a name.

We use the def keyword to define normal functions whereas to define anonymous functions we use the lambda keyword.

Anonymous functions are also known as lambda functions.

Syntax

The syntax for the lambda functions is as follows:

```Lambda arguments: expression;
```

Let’s try understanding how they work

Working

Let’s see how we can use lambda functions. To do this, first, we define a normal function and from which would give us a better reflection of the difference between lambda functions and normal functions

```def func(x):
return x+3;
print(func(2));
```

We can write the above function in terms of lambda functions as follows:

```func2=lambda x : x + 3;
print (func2 (2));
```

Both the codes return the same value and give the output as 3

Usage

We can also use these functions within another function as well. Let’s see how we can do it

```def func1 (x):
func2 = lambda x : x + 2;
return func2 (x);

print (func1 (2));
```

In the above code, we are creating a func1 which is a normal function and inside that block, we are creating func2 which is a lambda function. If you notice, the return value of func1 is the value computed by our lambda function func2.

The output for the above program would be 2.

We can also use lambda functions with multiple arguments. The following example explains it

```func1= lambda x, y : x + y;
print(func1 (2, 3));
```

Here the function func1 can accept two arguments and do the computation on those two arguments.

We can also use the lambda function as follows

```func1 = lambda x,y=3 : x+y;
print(func1(2));
```

Lambda functions usage expands to built-in functions like map(), filter(), reduce(). Let’s discuss these too.

Lambda functions in map()

map() function in python returns an iterator after the defined function is applied to each element present in the iterable.

The syntax for the map() is as follows:

`map(function, iterable)`

Iterable can be any group of elements like tuple, list, etc.

For function, we use the lambda function since we don’t have to make any special declaration or store the result in any sort of variable.

Let’s see the working of this

Code:

Python Code

``````list1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
list2 = list (map (lambda x: x+9, list1));

print (list2);
``````

Output:

[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Let’s take one more example

Code:

Python Code

``````list1 = [ 'striver','tuf','noogler','python' ];
list2 = list (map (lambda x: str.upper(x), list1));

print (list2);``````

Output:

[‘STRIVER’, ‘TUF’, ‘NOOGLER’, ‘PYTHON’]

Lambda functions in filter()

filter() function is used to test the given set of elements to check whether they return true or false for a given function.

The syntax for the filter() is as follows:

```filter(function, iterable);
```

Iterable can be any group of elements like tuple, list, etc.

For function, we use the lambda function since we don’t have to make any special declaration or store the result in any sort of variable.

Let’s see the working of this

Code:

Python Code

``````list1 = [2, 5, 12, 33, 42, 90, 88, 11];
list2 = list(filter(lambda x: (x % 2 != 0) , list1));
print(list2);
``````

Output:

[5, 33, 11]

Lambda functions in reduce()

reduce() function is used to apply the given function to all the elements in the given iterable.

It can be really useful when one needs to find the sum or product of elements in a list.

The syntax for reduce() function is as follows:

```reduce(function, iterable);
```

reduce() function is a part of functools library.

Iterable can be any group of elements like tuple, list, etc.

For function, we use the lambda function since we don’t have to make any special declaration or store the result in any sort of variable.

The lambda function used here accepts two parameters which clearly states that the function is applied to two objects and the result is stored. Later the function is applied to the stored result and the next succeeding object in the iterable.

Let’s see the working for this

Code:

Python Code

``````from functools import reduce
list1 = [1, 2, 3, 4, 5, 6, 7];
sum = reduce(lambda x,y: x + y,list1);
print(sum);
``````

Output: 28

Lambda functions can be said as short-hand for the normal functions. We can use normal functions in place of lambda functions in the map(), reduce(), and filter() but they increase the code length and may cause debugging issues for a new programmer.