Python Lambda Functions: A Practical Guide to Anonymous Functions
Python Lambda Functions: A Practical Guide to Anonymous Functions
Discover how Python’s anonymous lambda functions can simplify your code. This guide explains their purpose, syntax, and real‑world use cases with clear examples.
Video: Python Lambda
What are lambda functions in Python?
In Python, an anonymous function is defined without a name. While regular functions use the def keyword, anonymous functions are declared with lambda. Consequently, they are often called lambda functions.
How to use lambda Functions in Python?
Lambda functions follow a concise syntax:
Syntax of Lambda Function in Python
lambda arguments: expression
They can accept any number of arguments, but only one expression. The expression is evaluated and its result is returned. Because they return a function object, lambdas can be used wherever a function is required.
Example of Lambda Function in Python
Below is a simple lambda that doubles its input:
# Show a basic lambda function
double = lambda x: x * 2
print(double(5))
Output
10
Here, lambda x: x * 2 creates an unnamed function. The argument x is passed to the expression x * 2, which is evaluated and returned. The resulting function object is assigned to the variable double, allowing you to call it like a regular function. The following def equivalent illustrates the same logic:
def double(x):
return x * 2
When to Use Lambda Functions in Python
Lambda functions shine when you need a quick, throw‑away function, especially as an argument to higher‑order functions. They’re commonly paired with built‑in functions such as filter() and map() to perform concise transformations.
Example with filter()
The filter() function applies a predicate to each item in an iterable, returning only those that satisfy the condition. The following example keeps only even numbers from a list:
# Filter even numbers from a list
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x % 2 == 0), my_list))
print(new_list)
Output
[4, 6, 8, 12]
Example with map()
The map() function applies a transformation to each element, producing a new iterable. Here we double every element in a list:
# Double each item using map()
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(map(lambda x: x * 2, my_list))
print(new_list)
Output
[2, 10, 8, 12, 16, 22, 6, 24]
Python
- Master Python Functions: Syntax, Types, and Practical Examples
- Mastering Python Function Arguments: Positional, Keyword, and Default Parameters
- Mastering Python Recursion: How Functions Call Themselves
- Python Generators: Efficient Iteration and Advanced Use Cases
- Python Closures Explained: How Nested Functions Capture Variables
- Mastering Python Decorators: Enhance Functions with Expert Techniques
- Master Python Lambda Functions: Practical Examples & Best Practices
- Python round() Function Explained with Practical Examples
- Mastering Python's map() Function: Syntax, Examples, and Best Practices
- Python Functions Explained: Building Reusable Code Modules