Python Lambda

Python is a high-level programming language with a powerful feature set. One of these features is the ability to use lambda functions. Lambda functions, also known as anonymous functions, are small, single-expression functions that are not bound to a name. They can be used anywhere a function is required and are particularly useful when you need to create a function for a short period of time. In this blog, we will explore what lambda functions are and how they can be used in Python.

What are Python Lambda Functions?

In Python, lambda functions are small, anonymous functions that can be defined without a name. They are defined using the keyword “lambda” followed by a list of parameters and the function body. The function body is a single expression that is evaluated and returned by the lambda function. Here is the syntax for defining a lambda function in Python:

lambda arguments: expression

The arguments are a comma-separated list of parameters, and the expression is a single Python expression. When the lambda function is called, it will evaluate the expression and return the result.

Example Code Snippets

Let’s take a look at some examples of how lambda functions can be used in Python.

Example 1: Add Two Numbers

In this example, we will define a lambda function that adds two numbers.

add = lambda x, y: x + y
print(add(2, 3))  # Output: 5

In this code, we define a lambda function called “add” that takes two arguments, “x” and “y”, and returns their sum. We then call the lambda function with the arguments “2” and “3”, which results in “5” being printed to the console.

Example 2: Sort a List of Tuples

In this example, we will use a lambda function to sort a list of tuples based on the second element of each tuple.

students = [("John", 80), ("Alice", 90), ("Bob", 85)]

students.sort(key=lambda x: x[1])

print(students)  # Output: [("John", 80), ("Bob", 85), ("Alice", 90)]

In this code, we have a list of tuples representing students and their grades. We use the sort() method to sort the list based on the second element of each tuple, which represents the grade. We pass a lambda function to the key parameter of the sort() method, which returns the second element of each tuple (i.e., the grade). The resulting sorted list is then printed to the console.

Example 3: Filter a List

In this example, we will use a lambda function to filter a list of numbers based on whether they are even or odd.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

even_numbers = list(filter(lambda x: x % 2 == 0, numbers))

print(even_numbers)  # Output: [2, 4, 6, 8]

In this code, we define a list of numbers and use the filter() function to filter out the even numbers. We pass a lambda function to the filter() function, which returns True if the number is even and False otherwise. The resulting filtered list of even numbers is then printed to the console.

Conclusion

Lambda functions in Python are a powerful and flexible tool that can be used to create small, anonymous functions. They can be used in a variety of contexts, from sorting lists to filtering data. By understanding how to use lambda functions, you can write more concise and efficient code in your Python projects.