1. Introduction: What Is a Lambda Function?
In Python, writing a standard function using the def keyword requires naming the function, specifying its parameter list, and structuring an indented block. However, programs frequently require a function to execute only a single line of code to complete a small or trivial task.
Defining a full function with def and assigning it an explicit name for a single line of logic introduces unnecessary overhead. To solve this, Python provides lambda functions.
- Definition: A lambda function is a small, anonymous function (a function without a name) that is written entirely within a single line of code.
- Primary Use Case: Designed for quick, trivial operations where declaring a full
def block is redundant.
- Higher-Order Integration: Commonly paired with built-in Python functional tools like
map() and filter() to process lists and collections efficiently.
2. Syntax of a Lambda Function
A lambda function is constructed on one line using the lambda keyword:
lambda parameters: expression
lambda — The keyword signaling an anonymous, single-line function.
- Parameters — One or more input variables separated by commas.
- Colon (:) — Separates the parameter list from the operation.
- Expression — The single operation evaluated and automatically returned without writing an explicit
return keyword.
3. Practical Code Examples
Example 1: Adding Two Numbers (Multiple Parameters)
In this example, a lambda function accepts two parameters (a and b), adds them together, and stores the callable function inside a variable named add:
# 1. Defining a single-line lambda function with two parameters
add = lambda a, b: a + b
# 2. Calling the lambda function and printing the result
print(add(3, 5))
Output:
8
- The parameters
a and b receive 3 and 5.
- The expression adds them together and returns 8.
Example 2: Doubling a Single Number (Single Parameter)
A lambda function can also accept a single input to perform quick calculations, such as doubling a value:
# 1. Defining a lambda function to double a value
double = lambda x: x * 2
# 2. Passing 5 into the lambda function
print(double(5))
Output:
10
Passing 5 into the parameter x multiplies it by 2, outputting 10.
4. Using Lambda with Built-in Functions: map() and filter()
Example 3: Transforming Lists with map()
The map() function applies a specified function across every element in an iterable collection. If you have a list of numbers and need to compute the square of each item, you can pass a lambda function directly into map():
# 1. Define a list of numbers
numbers = [1, 2, 3, 4]
# 2. Use map() with a lambda function to square each number
squared_numbers = list(map(lambda x: x * x, numbers))
# 3. Print the transformed list
print(squared_numbers)
Output:
[1, 4, 9, 16]
The lambda function multiplies each number by itself (x * x). Applying it across [1, 2, 3, 4] produces [1, 4, 9, 16].
Example 4: Filtering Collections with filter()
The filter() function extracts elements from an iterable based on whether they satisfy a specific condition. Consider a list of numbers from 1 to 8. To extract only the odd numbers, a lambda function provides the filtering criteria using the modulus operator (%):
- Even numbers divided by 2 yield a remainder of 0.
- Odd numbers divided by 2 yield a non-zero remainder (
x % 2 != 0).
# 1. Original list from 1 to 8
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
# 2. Filter list for odd numbers using a lambda condition
odd_numbers = list(filter(lambda x: x % 2 != 0, numbers))
# 3. Print the filtered result
print(odd_numbers)
Output:
[1, 3, 5, 7]
The lambda function checks each element against the condition. Elements yielding a non-zero remainder are collected into odd_numbers.