Python Lambda Functions

In Python, lambda functions are small, anonymous functions defined using the lambda keyword. These functions can have any number of arguments but only one expression. Lambda functions are often used in situations where you need a quick function for a short period, such as in map, filter, or sorted functions.

Here are some common use cases for lambda functions:

  • Simple functions: Define small functions for short-term use.
  • In built-in functions: Use them as arguments for functions like map(), filter(), and sorted().
  • In functional programming: Use lambda to apply a function to items in a collection.


What You'll Learn

In this tutorial, you will learn how to use lambda functions in Python to write concise, one-line functions. We will cover how to define and use them in various situations.


Understanding the lambda Function

The syntax of a Python lambda function is:

python
lambda arguments: expression
  • lambda: The keyword used to define an anonymous function.
  • arguments: The parameters the function takes (optional). You can have multiple arguments separated by commas.
  • expression: The expression that is evaluated and returned when the lambda function is called. It can only be one expression.

Lambda functions do not require a name (hence "anonymous"). They are typically used where a simple, one-line function is needed temporarily.


Example 1: Basic Lambda Function

Here is a basic example of a lambda function that adds two numbers:

python
add = lambda x, y: x + y
print(add(5, 3))

How It Works:

  • lambda x, y:: Defines the lambda function that takes two arguments, x and y.
  • x + y: This is the single expression that gets evaluated and returned when the function is called.
  • add(5, 3): This calls the lambda function, passing 5 and 3 as arguments, and prints the result (8).

Output

8

Example 2: Lambda Function with map()

You can use lambda functions in combination with the map() function to apply a function to each item in an iterable.

python
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x**2, numbers)
print(list(squared))

How It Works:

  • map(): This function applies a given function to all items in an iterable.
  • lambda x: x**2: This lambda function squares each number in the list.
  • list(squared): This converts the map object to a list and prints it.

Output

[1, 4, 9, 16, 25]

Example 3: Lambda with filter()

You can use a lambda function with filter() to filter out items from an iterable based on a condition.

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

How It Works:

  • filter(): This function filters the elements of an iterable based on a given function.
  • lambda x: x % 2 == 0: This lambda function checks if each number is even.
  • list(even_numbers): Converts the filter object to a list and prints the even numbers.

Output

[2, 4, 6]

Exercises

Try out the following exercises to practice using the lambda function.

1. Write a lambda function that multiplies two numbers.
python
# Exercise 1: Multiply two numbers using lambda
multiply = lambda x, y: x * y
print(multiply(4, 5))

2. Write a lambda function that checks if a number is even.
python
# Exercise 2: Check if a number is even using lambda
is_even = lambda x: x % 2 == 0
print(is_even(10))

*Tip: After completing an exercise, feel free to experiment with lambda functions further to explore how they work!


Frequently Asked Questions

What is a lambda function in Python?

A lambda function is a short, anonymous function defined using the lambda keyword. It can take multiple inputs but contains only a single expression.


When should I use a lambda function?

Use lambda functions for short tasks, especially when passing a simple function to built-in functions like map(), filter(), or sorted().


How is a lambda function different from a regular function?

Lambda functions are defined in one line, anonymous, and limited to a single expression, while regular functions use def and can contain multiple lines and statements.


Can a lambda function have multiple arguments?

Yes! Lambda functions can accept multiple arguments, just like regular functions. Just separate them with commas within the parentheses.


Can I assign a lambda function to a variable?

Absolutely. You can assign a lambda function to a variable like square = lambda x: x ** 2 and use it as a normal function.



What's Next?

Next, you'll learn about the scope and lifetime of variables in Python. You'll discover how variable accessibility is determined by where it's defined—whether inside a function or globally—and how long a variable exists during the execution of a program. Understanding these concepts is key to writing clear and bug-free code.