Python - Lambda Functions
map()
function with lambda?The map()
function in Python is used to apply a specified function to all the items in an iterable (e.g., a list, tuple) and return an iterable (usually a list) with the results. When combined with a lambda
function, map()
becomes a powerful tool for concise and efficient data transformations.
Here's an example program that demonstrates the significance of the map()
function with lambda
:
# Using map() with lambda to square each element in a list
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
# Print the result
print(squared_numbers)
The output of the program will be:
[1, 4, 9, 16, 25]
In this example, the map()
function is applied to the list of numbers, and the lambda function lambda x: x**2
is used to square each element in the list. The result is a new list containing the squared values of the original numbers.
The significance of using map()
with lambda
lies in the ability to perform concise and expressive transformations on iterable data without the need for explicit loops or verbose code.
filter()
function in Python?In Python, the filter()
function is used to filter elements from an iterable based on a specified function. When combined with a lambda
function, filter()
becomes a powerful tool for selecting elements that meet certain criteria.
Here's an example program that demonstrates how to use lambda
functions with the filter()
function:
# Using filter() with lambda to select even numbers from a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
# Print the result
print(even_numbers)
The output of the program will be:
[2, 4, 6, 8, 10]
In this example, the filter()
function is applied to the list of numbers, and the lambda function lambda x: x % 2 == 0
is used to filter out only the even numbers. The result is a new list containing only the elements that satisfy the given condition.
The use of lambda
functions with filter()
provides a concise and readable way to perform conditional filtering on iterable data.
In Python, lambda functions play a crucial role in sorting lists, especially when a custom sorting criterion is needed. The sorted()
function and the sort()
method of lists can both leverage lambda functions for custom sorting.
Here's an example program that demonstrates the role of lambda functions in sorting lists:
# Sorting a list of tuples based on the second element using lambda
pairs = [(1, 5), (2, 3), (3, 8), (4, 1)]
sorted_pairs = sorted(pairs, key=lambda x: x[1])
# Print the result
print(sorted_pairs)
The output of the program will be:
[(4, 1), (2, 3), (1, 5), (3, 8)]
In this example, the sorted()
function is used to sort a list of tuples based on the second element of each tuple. The lambda function lambda x: x[1]
is employed as the key function, specifying the sorting criterion. This results in a new list, sorted_pairs
, where the tuples are sorted in ascending order based on their second elements.
Similarly, the sort()
method of lists can be used with a lambda function:
# Sorting a list of strings based on the length using lambda
words = ["apple", "banana", "kiwi", "orange"]
words.sort(key=lambda x: len(x))
# Print the result
print(words)
The output of the second part of the program will be:
['kiwi', 'apple', 'banana', 'orange']
In this case, the sort()
method is used to sort a list of strings based on their lengths, with the lambda function lambda x: len(x)
defining the sorting criterion.
Overall, lambda functions provide a concise way to define custom sorting criteria when working with lists in Python.
reduce()
function with lambda in Python.The reduce()
function in Python, available in the functools
module, is used to apply a rolling computation to a sequence of values. When combined with a lambda
function, reduce()
is a powerful tool for iteratively performing a specified operation on the elements of an iterable.
Here's an example program that demonstrates the concept of the reduce()
function with lambda
:
from functools import reduce
# Using reduce() with lambda to find the product of elements in a list
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
# Print the result
print(product)
The output of the program will be:
120
In this example, the reduce()
function is applied to the list of numbers, and the lambda function lambda x, y: x * y
is used to calculate the product of each pair of elements in the list. The reduce()
function then iteratively applies this lambda function to the accumulated result and the next element in the list, ultimately reducing the entire list to a single value, which is the product of all elements.
The reduce()
function is particularly useful for cumulative operations on iterable data, and when combined with a lambda
function, it provides a concise and expressive way to perform these computations.