Python is a powerful, multipurpose programming language that supports functional programming, among other paradigms. One of the unique features of Python is the use of anonymous functions, also known as lambda functions. The lambda
keyword is used to create anonymous functions, which can be used in various situations. In this tutorial, we’ll explore the lambda
keyword and how it can be used to create anonymous functions in Python.
What are Anonymous Functions?
Functions in Python are a set of instructions that perform a particular task. A function can be either named or anonymous. Anonymous functions are functions that are declared without a name. In Python, anonymous functions are defined using the lambda
keyword.
The lambda
keyword is used to create a small anonymous function object. It is used to create a one-time use function that can be passed as an argument to another function.
How to Use “lambda” to Create Anonymous Functions
The lambda
keyword is used to define a small anonymous function. The syntax of a lambda
function is as follows:
lambda arguments: expression
The arguments
are the inputs of the function, while expression
is the output of the function. The output of the function is returned by the lambda
keyword.
Unlike normal functions, lambda functions can have any number of arguments, but they can only have one expression.
Here’s an example of how to use the lambda
keyword to create an anonymous function in Python:
multiply = lambda x, y: x * y # Call the anonymous function print(multiply(2, 3)) # Output: 6
The lambda
keyword is used to create a small, one-line anonymous function that takes two arguments x
and y
, multiplies them, and returns the result.
In the example above, the multiply
variable is assigned to the anonymous function, which is then called with the multiply(2, 3)
statement.
Example: Using “lambda” with Sorting Functions
One of the most common use cases for lambda
functions is when working with sorting functions. The sorted()
function in Python can be used to sort lists, dictionaries, and other iterable objects. The sorted()
function takes an optional argument key
, which is a function to customize the sorting.
Here’s an example of how to use the lambda
keyword to create an anonymous sorting function:
fruits = [('apple', 10), ('banana', 5), ('pear', 2), ('kiwi', 20)] # Sort by the second element print(sorted(fruits, key=lambda x: x[1])) # Output: [('pear', 2), ('banana', 5), ('apple', 10), ('kiwi', 20)]
In the example above, the fruits
list is sorted by the second element of each tuple using a lambda function as a sorting key.
Example: Using “lambda” with Filter Functions
Another common use case for lambda
functions is when working with filter functions. The filter()
function in Python can be used to filter elements from a list or other iterable object.
Here’s an example of how to use the lambda
keyword to create a filter function:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Filter even numbers evens = list(filter(lambda x: x % 2 == 0, numbers)) print(evens) # Output: [2, 4, 6, 8, 10]
In the example above, the lambda
function is used to filter even numbers from the numbers
list using the filter()
function.
Conclusion
In this tutorial, we’ve explored the lambda
keyword and how it can be used to create anonymous functions in Python. Anonymous functions are useful in situations where a small, one-time use function is needed. The lambda
keyword can be used to create anonymous functions in sorting and filtering functions, among other use cases.