Python Lambda Functions (original) (raw)

Last Updated : 11 Nov, 2025

Lambda Functions are anonymous functions means that the function is without a name. As we already know def keyword is used to define a normal function in Python. Similarly, lambda keyword is used to define an anonymous function in Python.

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

Python `

s1 = 'GeeksforGeeks' s2 = lambda func: func.upper() print(s2(s1))

`

**Explanation: s2 is a lambda function that takes a string and returns it in uppercase. Applying it to 'GeeksforGeeks' gives the result.

Syntax

lambda arguments : expression

Use Cases of Lambda Functions

Let's see some of the practical uses of the Python lambda function.

1. Using with Condition Checking

A lambda function can include conditions using if statements.

Here, the lambda function uses nested if-else logic to classify numbers as Positive, Negative or Zero.

Python `

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

`

Output

Positive Negative Zero

**Explanation:

This lambda checks divisibility by 2 and returns "Even" or "Odd" accordingly.

Python `

check = lambda x: "Even" if x % 2 == 0 else "Odd" print(check(4))
print(check(7))

`

**Explanation:

2. Using with List Comprehension

Combining lambda with list comprehensions enables us to apply transformations to data in a concise way.

This code creates a list of lambda functions, each multiplying its input by 10 and then executes them one by one.

Python `

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

`

**Explanation:

3. Using for Returning Multiple Results

Lambda functions do not allow multiple statements, however, we can create two lambda functions and then call the other lambda function as a parameter to the first function.

The lambda calculates both sum and product of two numbers and returns them as a tuple.

Python `

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

`

**Explanation:

4. Using with filter()

filter() function in Python takes in a function and a list as arguments. This offers an elegant way to filter out all the elements of a sequence "sequence", for which the function returns True.

Here, the lambda is used as a filtering condition to keep only even numbers from the list.

Python `

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

`

**Explanation:

5. Using with map()

map() function in Python takes in a function and a list as an argument. The function is called with a lambda function and a new list is returned which contains all the lambda-modified items returned by that function for each item.

This code doubles each element of the list using a lambda function and returns a new list.

Python `

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

`

**Explanation:

6. Using with reduce()

reduce() function in Python takes in a function and a list as an argument. The function is called with a lambda function and an iterable and a new reduced result is returned. This performs a repetitive operation over the pairs of the iterable. The reduce() function belongs to the functools module.

Here, the lambda multiplies two numbers at a time and reduce() applies this across the whole list to calculate the product.

Python `

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

`

**Explanation:

Difference Between lambda and def Keyword

In Python, both lambda and def can be used to define functions, but they serve slightly different purposes. While def is used for creating standard reusable functions, lambda is mainly used for short, anonymous functions that are needed only temporarily.

Python `

Using lambda

sq = lambda x: x ** 2 print(sq(3))

Using def

def sqdef(x): return x ** 2 print(sqdef(3))

`

**Explanation:

Now, let’s see a comparison between these two in tabular form:

Feature lambda Function Regular Function (def)
**Definition Single expression with lambda. Multiple lines of code.
**Name Anonymous or named if assigned. Must have a name.
**Statements Single expression only. Can include multiple statements.
**Documentation Cannot have a docstring. Can include docstrings.
**Reusability Best for short, temporary functions. Better for reusable and complex logic.