Understanding Lambda Functions in Python | The Beauty of Anonymous Functions

In this Python tutorial, we will discuss Lambda functions in Python. Also, we will see, what is the use of the lambda function in Python. I will also explain, what is an anonymous function in Python.

What is a Lambda Function in Python?

Lambda functions, also known as anonymous functions, are a feature in Python that allows for the creation of small, simple functions in a concise manner. Unlike the traditional functions defined using the def keyword, lambda functions are defined using the lambda keyword, and they don’t have a name. This is why they are referred to as anonymous functions.

Syntax:

lambda arguments: expression

This means a lambda function can take any number of arguments, but can only have one expression.

Example:

multiply = lambda x, y: x * y

print(multiply(5, 3)) # Output: 15

In the example above, we create a lambda function that takes two arguments, x and y, and returns their product. We then assign this lambda function to a variable called multiply and call it just like a regular function.

See the output below:

what is lambda function in python
what is lambda function in python

Read: How to use the range() function to iterate through a list in Python

When and Why Use Lambda Functions in Python?

Lambda functions in Python are particularly useful when you need a small function for a short period of time and don’t want to formally define it. They’re concise and can be written in a single line. This makes them perfect candidates for scenarios where a function is only used once or for a short snippet of code.

Common use cases include:

  1. Sorting and Filtering: Lambda functions can be used as the key function while sorting or as the function to filter data.
  2. Functional Programming: Python supports functional programming concepts like map(), filter(), and reduce() where lambda functions are handy.

Examples

1. Sorting with Lambda:

data = [('apple', 3), ('banana', 1), ('orange', 4), ('grapes', 2)]

# Sort by the second element in each tuple
sorted_data = sorted(data, key=lambda x: x[1])

print(sorted_data) # Output: [('banana', 1), ('grapes', 2), ('apple', 3), ('orange', 4)]

You can see the output like below:

use of lambda function in python
use of lambda function in python

2. Filtering with Lambda:

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

# Get the odd numbers
odd_numbers = list(filter(lambda x: x % 2 != 0, numbers))

print(odd_numbers) # Output: [1, 3, 5, 7, 9]

3. Using Map with Lambda:

numbers = [1, 2, 3, 4, 5]

# Square each element
squared = list(map(lambda x: x ** 2, numbers))

print(squared) # Output: [1, 4, 9, 16, 25]

Understanding Anonymous Functions in Python

As we mentioned earlier, lambda functions are anonymous. This means they don’t have a name associated with them. They are useful when you need a function for a short duration and do not want to define it formally.

This concept is especially useful in functional programming. Here’s an example where we use an anonymous lambda function in Python inside the filter() function without assigning it to a variable.

# Filter out numbers less than 5
numbers = [2, 8, 3, 5, 7, 9, 1]
filtered_numbers = list(filter(lambda x: x < 5, numbers))

print(filtered_numbers) # Output: [2, 3, 1]

You can see the output below:

anonymous function in python
anonymous function in python

Conclusion

Lambda functions or anonymous functions in Python are powerful tools for writing cleaner, more concise code, especially for short, simple functions. They are versatile and can be used in various scenarios like sorting, filtering, and other functional programming paradigms. However, keep in mind that while lambda functions make code shorter, overuse can lead to less readable code.

You may like the following Python tutorials: