Lambda functions are small anonymous functions defined with the lambda
keyword. They are often used for short-term tasks that do not require a full function definition. Here's a guide to help you understand and use lambda functions effectively.
The basic syntax of a lambda function is:
lambda arguments: expression
The lambda
keyword is followed by a list of arguments, a colon, and an expression. The expression is evaluated and returned.
add = lambda x, y: x + y
print(add(2, 3))
lambda
keyword. It is generally used for short, simple operations and can be defined in a single line. A regular function, on the other hand, is defined using the def
keyword, can contain multiple lines of code, and can be reused throughout the program. Lambda functions are limited to a single expression and do not have their own function name or documentation strings.
Lambda functions are often used with built-in functions like map()
, filter()
, and reduce()
.
map()
The map()
function applies a given function to all items in an input list.
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x ** 2, numbers)
print(list(squared))
map()
function work with a lambda function?
map()
function takes two arguments: a function and an iterable (like a list). It applies the function to each item in the iterable and returns a map object, which can be converted to a list. When used with a lambda function, the lambda function is applied to each element in the iterable. In Example 2, the lambda function squares each number in the list numbers
.
filter()
The filter()
function filters items in an iterable based on a condition.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers))
filter()
function return when used with a lambda function?
filter()
function returns an iterator that contains only the items from the iterable for which the lambda function returns True
. In Example 3, the lambda function checks if each number is even. The filter()
function returns an iterator with only the even numbers from the list numbers
.
reduce()
The reduce()
function, from the functools
module, applies a function cumulatively to the items of an iterable, reducing it to a single value.
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product)
reduce()
function work with a lambda function?
reduce()
function takes two arguments: a function and an iterable. It applies the function cumulatively to the items in the iterable, from left to right, so as to reduce the iterable to a single value. When used with a lambda function, the lambda function specifies how the items should be combined. In Example 4, the lambda function multiplies the numbers together, resulting in the product of all the numbers in the list numbers
.
Lambda functions can be used as the key in sorting functions.
points = [(2, 3), (1, 2), (4, 1), (3, 5)]
points_sorted = sorted(points, key=lambda point: point[1])
print(points_sorted)
sorted()
?
sorted()
function can take a key argument, which is a function that extracts a comparison key from each list element. When a lambda function is used as the key, it defines the sorting criteria. In Example 5, the lambda function lambda point: point[1]
extracts the second element of each tuple for comparison, resulting in the list being sorted based on the second element of the tuples.
Lambda functions are a powerful feature in Python that allow for concise function definitions. They are especially useful in functional programming techniques such as using map()
, filter()
, and reduce()
. Understanding how to use lambda functions can help you write more efficient and readable code.