Lambda functions in Python, also known as anonymous functions, are small, unnamed functions defined using the lambda
keyword. These functions can have any number of arguments but only one expression. They are syntactically restricted to a single line and are used primarily for creating small, throwaway functions.
Syntax of Lambda Functions
The syntax for a lambda function is:
lambda arguments: expression
- lambda: This keyword is used to create a new lambda function.
- arguments: A comma-separated list of arguments that the function accepts.
- expression: An expression that the function evaluates and returns. The expression must be a single line.
Defining and Using Lambda Functions
Basic Example
Here’s a simple example of a lambda function that adds two numbers:
add = lambda x, y: x + y result = add(2, 3) print(result) # Output: 5
Using Lambda Functions Inline
Lambda functions are often used in places where a function is required temporarily, often in higher-order functions like map()
, filter()
, and sorted()
.
Example with map()
The map()
function applies a given function to each item in an iterable (like a list) and returns a map object (an iterator).
numbers = [1, 2, 3, 4, 5] squared = map(lambda x: x ** 2, numbers) print(list(squared)) # Output: [1, 4, 9, 16, 25]
Example with filter()
The filter()
function constructs an iterator from elements of an iterable for which a function returns true.
numbers = [1, 2, 3, 4, 5] even_numbers = filter(lambda x: x % 2 == 0, numbers) print(list(even_numbers)) # Output: [2, 4]
Example with sorted()
The sorted()
function returns a new sorted list from the elements of any iterable.
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] # Sorting by the second element of the tuple sorted_pairs = sorted(pairs, key=lambda pair: pair[1]) print(sorted_pairs) # Output: [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Using Lambda Functions with reduce()
The reduce()
function from the functools
module repeatedly applies a function to the elements of an iterable, reducing it to a single value.
from functools import reduce numbers = [1, 2, 3, 4, 5] sum_numbers = reduce(lambda x, y: x + y, numbers) print(sum_numbers) # Output: 15
Example: Sorting with Lambda Functions
Lambda functions can be particularly useful for custom sorting.
students = [ {'name': 'John', 'age': 25, 'grade': 88}, {'name': 'Jane', 'age': 22, 'grade': 92}, {'name': 'Dave', 'age': 23, 'grade': 85}, ] # Sort by age sorted_by_age = sorted(students, key=lambda student: student['age']) print(sorted_by_age) # Output: [{'name': 'Jane', 'age': 22, 'grade': 92}, {'name': 'Dave', 'age': 23, 'grade': 85}, {'name': 'John', 'age': 25, 'grade': 88}]
# Sort by grade sorted_by_grade = sorted(students, key=lambda student: student['grade']) print(sorted_by_grade) # Output: [{'name': 'Dave', 'age': 23, 'grade': 85}, {'name': 'John', 'age': 25, 'grade': 88}, {'name': 'Jane', 'age': 22, 'grade': 92}]
Lambda functions in Python provide a concise way to create small, unnamed functions at runtime. They are particularly useful in scenarios where you need a simple function for a short period, such as in functional programming constructs like
map()
, filter()
, and reduce()
. While powerful, it's often best to use them for simple operations to keep the code readable and maintainable. For more complex functions, defining a standard function with def
is recommended.
Comments
Post a Comment