The Power of Lambda Expressions in Python | by Soner Yıldırım | Towards Data Science

A function is a block of code that takes zero or more inputs, performs some operations, and returns a value. Functions are essential tools to create efficient and powerful programs.
In this article, we will cover a special form of functions in Python: lambda expressions. The first and foremost point we need to emphasize is that a lambda expression is a function.
square = lambda x: x**2type(square)functionsquare(5)25
The square is a function that returns the square of a number. In the traditional form of defining functions in Python, the square function would look as below.
def square(x):   return x**2
Why do we need a different way of defining a function? The main motivation behind the lambda expressions is the simplicity and practicality.
Consider an operation that needs to be done once or very few times. Furthermore, we have many variations of this operation which are slightly different than the original one. In such case, it is not ideal to define a separate function for each operation. Instead, lambda expressions provide a much more efficient way of accomplishing the tasks.
A key characteristic of lambda expressions is that they are nameless functions. You might argue that we have actually assigned a name to the lambda expression (square) but it was for demonstration purposes. In general, lambda expressions are used without a name.
One common use case for lambda expressions is that they can be passed as argument to another function. The map, reduce, and filter functions in Python are higher-order functions that can accept other functions as arguments.
Let’s do an example for each and see how lambda expressions come in handy. We have the following list a.
a = [1, 3, 2, 6, 7, 4]
The reduce function reduces the list by applying a function to its elements. We can write a reduce function that multiplies elements in the list.
from functools import reducereduce(lambda x, y: x*y, a)1008
The map function creates a mapping to transform each element in the list. For instance, the following map function squares each element in the list based on the given lambda expression.
list(map(lambda x: x*x, a))[1, 9, 4, 36, 49, 16]
The filter function operates similar to the map function. Instead of transforming elements, it filters them based on the given condition.
list(filter(lambda x: x > 4, a))[6, 7]
The lambda expression in the filter function serves as a filter the elements greater than 4.
Lambda expressions are also used with Pandas data manipulation and transformation functions. Consider the following dataframe.
For instance, we can take the log of the columns using the apply function and a lambda expression.
import numpy as npimport pandas as pddf.apply(lambda x: np.log(x))
We can find the maximum value in each row using the following lambda expression and apply function.
df.apply(lambda x: x.max(), axis=1)0    15.01    19.02    15.03    19.04    12.0dtype: float64
If we change the axis parameter to 0, the function will return the maximum value in each column.
df.apply(lambda x: x.max(), axis=0)cola     9.00colb    19.00colc     0.76dtype: float64
The apply function of pandas performs the given operation columns-wise or row-wise depending on the specified axis.
Pandas also provides the applymap function which allows to apply a given operation to all the elements in a dataframe. We can also pass a lambda expression to the applymap function.
Consider the following dataframe.
We can find the length of strings in each cell by using a lambda expression with the applymap function.
df.applymap(lambda x: len(x))
Lambda expressions are great to use as a replacement for simple functions. They also simplify the syntax. However, it is not ideal to use lambda expressions all the time.
Lambda expressions should not be used to perform complex operations. Otherwise, we would violating the purpose.
Lambda expression are great fit for tasks that are done once or very few times. If we need to do an operation several times throughout the code, it is better to explicitly define a function.
Thank you for reading. Please let me know if you have any feedback.