The Python Lambda function is an anonymous function. It means, a function without a definition keyword and name. In order to create a lambda function, you have to use lambda keyword.

In this article, we will show you, How to use Lambda function or lambda expression in Python Programming language with multiple examples.

## Python Lambda Syntax

The syntax of the Lambda expression in Python Programming Language is as shown below:

lambda arguments:expression

The lambda function accepts any number of arguments. However, it can only accepts one expression.

For instance lambda a, b: a + b. Here, a and b are the arguments accepted by the lambda function. a + b is the expression.

## Python Lambda Examples

The following list of examples helps you understand the Python lambda functions.

### Python Lambda with No Arguments

You can use the following lambda statement to display the True or False as an output. This can make you understand that, you don’t need any argument to create a lambda expression.

# Python Lambda Example

num = lambda: True

print(num())

**OUTPUT**

### Python Lambda Sum

In this example, we are using the lambda expression to add 5 to the given argument value.

This code accepts one value because we specified one argument after the **lambda** keyword. After the colon, it’s an expression or functionality it has to perform when you call this anonymous function.

num = lambda x: x + 5

print(num(10))

**OUTPUT**

You can see from the below image, we passed 10 as an argument. 10 + 5 = 15

In this Python Lambda Sum example, we are using two arguments to perform addition or finding sum of two numbers. It means, you have to assign two values while calling this expression.

add = lambda x, y : x + y

print(add(10, 20))

**OUTPUT**

Generally, you can achieve the same by declaring or creating a function. I suggest you refer to Functions article, to understand the concept of declaration.

This time, we are using the lambda expression and the regular function. As you know, both will give the same result.

add = lambda x, y : x + y

print(add(10, 20))

print("\nResult from a Function")

def add_func(x, y):

return x + y

print(add_func(10, 20))

**OUTPUT**

If you observe the below screenshot, they both are returning the same result. However, regular function need def keyword, function name, and a return value. Where as, lambda function doesn’t need any of them. By default, it returns the expression result.

Lambda expressions are not about adding two values. You can do multiplication, subtractions or any other calculations. Here, we are multiplying two argument values. For better understanding, we are placing the regular function as well.

multi = lambda x, y : x * y

print(multi(5, 20))

print("\nResult from a multi Function")

def multi_func(x, y):

return x * y

print(multi_func(5, 20))

**OUTPUT**

### Python lambda Mulitple Values

In this example, we are using three arguments. Next, we are multiplying those three argument values.

multi = lambda x, y, z : x * y * z

print(multi(5, 2, 6))

print("\nResult from a multi Function")

def multi_func(x, y, z):

return x * y * z

print(multi_func(5, 2, 6))

**OUTPUT**

Until now, we used this lambda expression to calculate something and returning results. However, you can use the print statement to print the output as well. Below query prints Hello World! as an output.

message = lambda: print("Hello World!")

message()

**OUTPUT**

You can also achieve the same result by calling that lambda statement with parenthesis.

### Python Lambda Default Argument Values

In this example, we assigned default values to all three arguments. Next, we are adding, multiplying, and subtracting them.

If you have the default values then you don’t have to pass values while calling the lambda expression.

# Python Lambda Example

add = lambda x = 10, y = 20, z = 30 : x + y + z

print(add()) # 10 + 20 + 30

multi = lambda x = 10, y = 20, z = 30 : x * y * z

print(multi()) # 10 * 20 * 30

sub = lambda x = 10, y = 45: y - x

print(sub()) # 45 - 10

**OUTPUT**

However, you can override the default values by passing new values as the arguments. Here, the first print statement overrides 10 with 12, 20 with 14 and 30 with 16. It means, x = 12, y = 14 and z = 16.In the second statement, x = 75, y = 126 and z = 30.

add = lambda x = 10, y = 20, z = 30 : x + y + z

print(add(12, 14, 16)) # 12 + 14 + 16

print(add(75, 126)) # 75 + 126 + 30

print(add(222)) # 222 + 20 + 30

print(add()) # 10 + 20 + 30

print("Multiplication Values")

multi = lambda x = 10, y = 20, z = 30 : x * y * z

print(multi(2, 4, 5)) # x = 2, y = 4, z = 5

print(multi(100, 22)) # x = 100, y = 22, z = 30

print(multi(9)) # x = 9, y = 20, z = 30

print(multi()) # 10 * 20 * 30

**OUTPUT**

### Python Lambda Without Arguments

If you don’t want to pass any arguments but, you want to return something then you can use this kind of statement.

You don’t have to pass any argument values to call these lambda expressions. Whenever you call them, they will return the same result.

add = lambda : 10 + 20

print(add())

print("Multiplication Values")

multi = lambda : 10 * 20

print(multi())

print("Subtraction")

sub = lambda : 225 - 20

print(sub())

**OUTPUT**

### Python Anonymous Functions using Lambda

More than a replacement of regular functions, Python Lambda function is very powerful when you use this anonymous function inside a function.

In this example, we declared a function that accepts one argument. Inside the function, we used lambda expression to multiply that value with unknown number of times.

# Python Anonymous Functions Lambda Example

def new_func(n):

return lambda x : x * n

number = new_func(2)

print(number(50))

**OUTPUT**

**ANALYSIS**

Below statement calls new_func function where n = 2. It means, function returns lambda x: x * 2. Then, we assigned that value to number (lambda object)

number = new_func(2)

Next, within the print statement, we called that number with 50 (this is lambda argument value. It means, lambda 50 : 50 * 2 => 100

This time, we are calling the function with different values. First with 2, and then with 3.

def new_func(n):

return lambda x : x * n

number1 = new_func(2)

number2 = new_func(3)

print(number1(50))

print(number2(50))

**OUTPUT**

## Python Lambda with Built-in Functions

In python, you can use the available Built in functions along with lambda function.

### Python Lambda Filter Example

In this example, we are going to use the built-in filter function to filter the sequence of list items. First, we declared a list of numbers from 1 to 15. Next, we used the filter function with lambda expression.

This lambda statement checks whether number is divisible by 2 or not. Next, filter function returns all the True values.

**TIP:** I suggest you to refer the Python List article to understand the concept of list.

number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

print(number)

new_num = list(filter(lambda x : x % 2 == 0, number))

print(new_num)

**OUTPUT**

Here, we extended the previous example. This code filters and returns the Even Number, Odd Numbers, and Numbers that are divisible by 3.

number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

print(number)

even_num = list(filter(lambda x : x % 2 == 0, number))

print(even_num)

odd_num = list(filter(lambda x : x % 2 != 0, number))

print(odd_num)

num_div_by_3 = list(filter(lambda x : x % 3 == 0, number))

print(num_div_by_3)

**OUTPUT**

### Python Lambda Map Example

Unlike filter function, **map function** takes each list item and returns both the True and False values. In this example, we used map function to return the boolean value. It checks each individual value is divisible by 2 equals to 0. If true, it returns True otherwise, false was returned.

number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print(number)

new_num = list(map(lambda x : x % 2 == 0, number))

print(new_num)

**OUTPUT**

This time, we are performing multiplication using this map function. It will take one individual list item at a time, and performs the multiplication. At last, it returns the modified list.

number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print(number)

double_num = list(map(lambda x : x * 2, number))

print(double_num)

triple_num = list(map(lambda x : x * 3, number))

print(triple_num)

square_num = list(map(lambda x : x ** 2, number))

print(square_num)

**OUTPUT**

By using map function, you can also perform calculations on multiple lists. For instance, this example performs addition, subtraction and multiplication of two lists. It takes one value from both the list at same position and perform calculation.

number1 = [10, 20, 30]

number2 = [15, 25, 35]

print(number1)

print(number2)

print("***********==========*************")

add_num = list(map(lambda x, y : x + y, number1, number2))

print(add_num)

sub_num = list(map(lambda x, y : x - y, number1, number2))

print(sub_num)

mul_num = list(map(lambda x, y : x * y, number1, number2))

print(mul_num)

**OUTPUT**

**ANALYSIS**

add_num = list(map(lambda x, y : x + y, number1, number2))

First, x = 10 from number1, y = 15 from number2 list. By adding both of them get 25. Do the same for the remaining list items.

### Python Lambda Reduce Example

The Python reduce function accepts two values and a list as an arguments values. In this example, we are going to use this function along with lambda function.

from functools import reduce

number = [10, 20, 30, 15, 25, 35, 45]

print(number)

print("***********==========*************")

add_num = reduce((lambda x, y : x + y), number)

print(add_num)

sub_num = reduce((lambda x, y : x - y), number)

print(sub_num)

mul_num = reduce((lambda x, y : x * y), number)

print(mul_num)

**OUTPUT**

**ANALYSIS**

number = [10, 20, 30, 15, 25, 35, 45]

add_num = reduce((lambda x, y : x + y), number)

First, x = 10, y = 20. Or you can simply write it as ((((((10 + 20) + 30) + 15) + 25) + 35) + 45)

### Python Lambda Built-in function Example

Until now, we are using the lambda expression to calculate something or performing numerical operations. However, you can use them on string data as well.

In this example, we declared a string list. Next, we used the sorted function to sort the list items. However, we used the sorted key as a lambda expression. Within this expression, we are using len function to find the length of each word. It means, sorting will be done based on the item length.

x = ['apple', 'Mango Fruit', 'Banana', 'oranges', 'cherry','kiwi']

print(x)

y = sorted(x, key = lambda a: len(a))

print(y)

z = sorted(x, key = lambda a: a.casefold())

print(z)

**OUTPUT**

Thank You for Visiting Our Blog