Python > Core Python Basics > Functions > Lambda Functions (anonymous functions)
Lambda Function Example: Squaring a Number
This snippet demonstrates the use of a lambda function to square a number. Lambda functions are small, anonymous functions defined using the lambda
keyword. They are often used for simple operations that can be expressed in a single line.
Basic Lambda Function Syntax
The code defines a lambda function named square
that takes one argument x
and returns its square. The lambda
keyword is followed by the argument list (x
), a colon (:
), and the expression to be evaluated (x * x
). The function is then called with the argument 5
, resulting in the output 25
.
square = lambda x: x * x
print(square(5)) # Output: 25
Concepts Behind the Snippet
Lambda functions are anonymous, meaning they don't have a name like regular functions defined with def
. They are expressions, not statements, which means they can be used inline in places where a function is expected but a full function definition would be cumbersome. They implicitly return the result of the expression. Key characteristics are their brevity and suitability for simple, single-expression operations.
Real-Life Use Case
Lambda functions are commonly used with functions like map()
, filter()
, and sorted()
to perform operations on iterables (lists, tuples, etc.) concisely. For example, you can use a lambda function with map()
to apply a function to each element of a list.
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x * x, numbers))
print(squared_numbers) # Output: [1, 4, 9, 16, 25]
Best Practices
Use lambda functions for short, simple operations that can be expressed in a single line. Avoid complex logic within a lambda function, as it can reduce readability. If the logic is more involved, it's better to define a regular function with def
.
Interview Tip
Be prepared to explain the difference between lambda functions and regular functions defined with def
. Highlight the use cases where lambda functions are most appropriate, emphasizing their conciseness and suitability for simple, inline operations. Understand when to use them and when not to.
When to use them
Lambda functions are ideal when you need a small function for a short period, particularly as an argument to higher-order functions. This avoids the overhead of defining a fully-fledged function when it is only used once and in a specific context.
Memory footprint
Lambda functions often have a smaller memory footprint compared to regular functions because they are defined inline and are typically used for simple tasks. However, the difference in memory usage is usually negligible for most applications.
Alternatives
The alternative to a lambda function is a regular function defined with def
. If the function logic is complex or requires multiple statements, a regular function is generally a better choice for readability and maintainability.
Pros
Conciseness: Lambda functions allow you to define functions in a single line, making the code more compact.
Readability (in simple cases): For simple operations, lambda functions can improve code readability by avoiding the need for a separate function definition.
Inline Usage: They can be used inline in places where a function is expected.
Cons
Limited to single expression: Lambda functions can only contain a single expression, which limits their complexity.
Readability (in complex cases): Complex lambda functions can become difficult to read and understand.
No statements: Lambda functions cannot contain statements like if
, for
, or while
.
FAQ
-
What is the difference between a lambda function and a regular function?
Lambda functions are anonymous, single-expression functions defined inline using thelambda
keyword. Regular functions are defined withdef
and can contain multiple statements. Lambda functions are typically used for simple operations, while regular functions are suitable for more complex logic. -
Can a lambda function have multiple arguments?
Yes, a lambda function can have multiple arguments, separated by commas. For example:add = lambda x, y: x + y
. -
Can I use a lambda function with a conditional statement?
You cannot use traditional conditional statements (likeif
/else
) directly within a lambda function. However, you can use a conditional expression (also known as a ternary operator) within a lambda function. For example:max_value = lambda x, y: x if x > y else y
.