Python Lambda Functions (original) (raw)

Last Updated : 18 May, 2026

Lambda functions are small anonymous functions, meaning they do not have a defined name. These are small, short-lived functions used to pass simple logic to another function.

In this example, a lambda function is defined to convert a string to its upper case using upper().

Python `

a = 'GeeksforGeeks' upper = lambda x: x.upper()
print(upper(a))

`

**Explanation:

Syntax

Lambda functions are created using the lambda keyword. Below is the syntax:

lambda

Python Lambda Expression

Use Cases

**1. Condition Checking: lambda function can use conditional expressions (if-else) to return different results based on a condition.

Python `

check = lambda x: "Positive" if x > 0 else "Negative" if x < 0 else "Zero" print(check(5))
print(check(-3))
print(check(0))

`

Output

Positive Negative Zero

**Explanation: lambda function takes x as input. It uses nested if-else statements to return "Positive," "Negative," or "Zero."

**2. List Comprehension: Lambda can be combined with list comprehensions to apply the same operation to multiple values in a compact way.

Python `

func = [lambda arg=x: arg * 10 for x in range(1, 5)] for i in func: print(i())

`

**Explanation: lambda function multiplies each element by 10. List comprehension iterates through range(1, 5) and applies the lambda to each value.

**3. Returning Multiple Results: Although a lambda can contain only one expression, it can still return multiple results by combining them into a tuple.

Python `

calc = lambda x, y: (x + y, x * y) res = calc(3, 4) print(res)

`

**Explanation: lambda function performs both addition and multiplication and returns a tuple with both results.

**4. filter():This function uses a lambda expression to select elements from a list that satisfy a given condition, such as keeping only even numbers.

Python `

c = [1, 2, 3, 4, 5, 6] even = filter(lambda x: x % 2 == 0, c) print(list(even))

`

**Explanation: lambda function checks if a number is even (x % 2 == 0) and filter() applies this condition to each element in the list c.

**5. map(): This function applies a lambda expression to each element and returns a map object. It can be converted to a list using list().

Python `

a = [1, 2, 3, 4] double = map(lambda x: x * 2, a) print(list(double))

`

**Explanation: lambda function doubles each number and map() iterates through a and applies the transformation.

**6. reduce(): This function repeatedly applies a lambda expression to elements of a list to combine them into a single result.

Python `

from functools import reduce a = [1, 2, 3, 4] mul = reduce(lambda x, y: x * y, a) print(mul)

`

**Explanation: lambda multiplies two numbers at a time and reduce() applies this operation across the list.

To know the difference between def keyword and Lambda function, refer to this article, def vs lambda.