Let’s look at the examples
The basic syntax of a lambda function is as follows:
lambda arguments: expression
- lambda: The keyword used to define a lambda function.
- arguments: The input parameters the function takes (similar to the arguments of a regular function).
- expression: The operation performed by the function, and its result is implicitly returned.
Simple example
multiply = lambda x, y: x * y result = multiply(3, 4) print(result)
In this example, the lambda function lambda x, y: x * y takes two arguments x and y and returns their product. The result of calling this lambda function with arguments 3 and 4 is 12.
Lambda functions are often used in situations where a small, one-time function is needed, such as in the context of functions like map, filter, or sorted. They are a powerful tool for creating quick, inline functions without the need for a formal function definition.
Lambda with if
# Example 1: Filtering even numbers using lambda with if even_numbers = list(filter(lambda x: x % 2 == 0, range(1, 11))) print(even_numbers) # Example 2: Using lambda with if-else to categorize numbers category = lambda x: 'Even' if x % 2 == 0 else 'Odd' result = category(5) print(result)
Lambda with a loop
# Example: Multiplying each element of the list by 2 using lambda with map original_list = [1, 2, 3, 4, 5] doubled_list = list(map(lambda x: x * 2, original_list)) print(doubled_list)
Lambda with lists
# Example: Filtering only even numbers using lambda and list comprehension numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers)
Lambda with multiple lines
# Example: Lambda function with multiple lines to calculate the average average = lambda x: ( sum(x) / len(x) ) if len(x) > 0 else None numbers = [10, 20, 30, 40, 50] result = average(numbers) print(result)
Lambda with map and zip
# Example: Combining two lists using lambda, map, and zip list1 = [1, 2, 3] list2 = [4, 5, 6] result = list(map(lambda x, y: x + y, list1, list2)) print(result)
The map function applies the lambda function to each pair of corresponding elements in list1 and list2. In this case, the lambda function is lambda x, y: x + y, which adds up the corresponding elements of x and y.
list1 = [1, 2, 3] list2 = [4, 5, 6] result = list(map(lambda pair: pair[0] + pair[1], zip(list1, list2))) print(result)
The zip function creates an iterator that aggregates elements from list1 and list2 into pairs, and the map function applies the lambda function to each pair of elements resulting from zip. The result is the same as in the code above.
Lambda with reduce
from functools import reduce # Example: Cumulative multiplication of elements in a list using lambda and reduce numbers = [2, 3, 4, 5] product = reduce(lambda x, y: x * y, numbers) print(product)
Lambda with default arguments
# Example: Lambda function with default arguments power = lambda base, exponent=2: base ** exponent result = power(3) print(result)
Lambda with conditional expressions
# Example: Conditional expressions in a lambda function check_number = lambda x: 'Positive' if x > 0 else 'Non-Positive' result = check_number(-5) print(result)
Lambda with string manipulation
# Example: Capitalizing each word in a list using lambda and map words = ['python', 'lambda', 'examples'] capitalized_words = list(map(lambda x: x.capitalize(), words)) print(capitalized_words)
Conclusion
Empower your Python programming journey with the flexibility and brevity of lambda functions. Embrace the efficiency they bring to your IF checks, loop iterations, list operations, and beyond. As you integrate these techniques into your coding practices, you’ll find yourself crafting more elegant and expressive Python code. Keep exploring, experimenting, and leveraging the power of lambda functions to take your Python programming to new heights. Happy coding!