Python tutorials > Core Python Fundamentals > Functions > What are lambda functions?
What are lambda functions?
Lambda functions, also known as anonymous functions, are small, single-expression functions in Python. They are defined using the lambda
keyword and are often used for creating simple functions inline without needing a formal function definition with a name and the def
keyword.
Basic Syntax and Example
The basic syntax of a lambda function is lambda arguments: expression
. In the example above, lambda x, y: x + y
defines a lambda function that takes two arguments (x and y) and returns their sum. The result of the lambda function is assigned to the variable add
. Then, add(5, 3)
calls the lambda function with the arguments 5 and 3, resulting in 8.
add = lambda x, y: x + y
print(add(5, 3)) # Output: 8
Concepts Behind the Snippet
Key concepts to understand about lambda functions:
return
, pass
, or assignments.
Real-Life Use Case: Sorting with Lambda
A common use case for lambda functions is with the sort()
method or the sorted()
function. The key
argument of these functions takes a callable (a function). Lambda functions are ideal here because you often need a simple function to specify the sorting criteria without defining a separate named function. In this example, the data is sorted based on the second element (index 1) of each tuple.
data = [(1, 'z'), (2, 'a'), (3, 'b')]
data.sort(key=lambda item: item[1])
print(data) # Output: [(2, 'a'), (3, 'b'), (1, 'z')]
Real-Life Use Case: Using lambda function with map()
Another common use case is using with map()
. The map()
function applies a given function to each item of an iterable and returns a list of the results. Lambda functions are useful here for providing the function to be applied to each element concisely. In this example, each number in the list is squared.
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
print(squared_numbers)
Best Practices
Here are some best practices when using lambda functions:
Interview Tip
When discussing lambda functions in an interview, be sure to highlight their role in simplifying code, particularly when used with functions like map()
, filter()
, and sort()
. Demonstrate your understanding of their limitations (single expression) and when they are most appropriate. Also, be prepared to explain when a named function would be a better choice for readability and maintainability.
When to Use Them
Use lambda functions when:
map()
, filter()
, sort()
).
Memory Footprint
The memory footprint of a lambda function is generally comparable to that of a regular function. Because the function is only defined briefly for a single purpose, this is generally good for efficiency.
Alternatives
The alternative to lambda functions is defining a regular function using the def
keyword. If the function logic is complex, a regular function is usually a better choice for readability and maintainability.
# Lambda function:
add = lambda x, y: x + y
# Equivalent regular function:
def add(x, y):
return x + y
Pros
Advantages of using lambda functions:
Cons
Disadvantages of using lambda functions:
FAQ
-
Can I use multiple statements in a lambda function?
No, lambda functions are limited to a single expression. If you need multiple statements, use a regular function defined with thedef
keyword. -
Are lambda functions more efficient than regular functions?
The performance difference is usually negligible. The primary advantage of lambda functions is their conciseness and inline definition, not necessarily performance. -
Can I use a
return
statement inside a lambda function?
No, you cannot use areturn
statement in a lambda function. The result of the expression is implicitly returned.