Working with Anonymous Functions
- Andrew Cole
- Apr 13, 2020
- 3 min read
Save yourself time with these simple operation shortcuts

Photo by Tarik Haiga on Unsplash
When we are manipulating our data we very seldom will leave it untouched, untransformed, or unaggregated. Data has to be cleaned, converted, summarized, and described all in an efficient manner to prepare the data for implementation. Creating functions is a standard and robust practice which can save time when applying pre-defined operations to your data. However, although defining your own function is vitally important at times, other times it is simply unnecessary. We can use a special type of function to a particular series to expedite the process without sacrificing computational efficiency or accuracy.
Anonymous Functions
We implement some functions in Python without giving them a name (thus, anonymous). These functions are intended only to be used one time at a specific moment of your data manipulation process. Introducing, the lambda function:
lambda <arguments>: <expression>
Above is the general syntax of a lambda function. We will go into further depth in just a moment, but first let’s define the syntax.
Lambda functions can have any number of arguments (what will be operated on), but only a single expression (what will be evaluated and returned)
A lambda function can be applied anywhere that a function object is required
Restricted to a singular expression
Let’s take a look comparing an anonymous lambda function with a traditional defined function object when trying to double all contents of a list:
Traditional:
list1 = [1, 2, 3]
def double_items(list1):
list2 = []
for x in list1:
list2.append(x*2)
return list2
Which returns:
double_items(list1)
Output: [2,4,6]
Lamba:
list1 = [1,2,3]
list2 = list(map(lambda x: x*2, list1))
Which returns:
print(list2)
Output: [2,4,6]
We can clearly see that the second lambda function allows for us to accomplish the same computation in a single line of code, instead of needing to define unnecessary code blocks.
Lambda Scenarios:
There are three main scenarios to which lambda anonymous functions can be applied: filter(), map(), and reduce().
Filter()
This function will take a list as its arguments and then filter out the list’s elements which will return “true” for the expression. The function we see below will only return the even list items (‘%’ modulus operator used).
ex_list = [33, 2, 42, 1, 47, 430, 23, 98, 12]
final = list(filter(lambda x: (x%2 == 0), ex_list))
Output:
print(final)
[2, 42, 430, 98, 12]
Map()
In my experience working with data, this function is by far the most used anonymous function. We already used map in the original example. The map function is very efficient as well when being used in conjunction with a pandas series object. Map is a function that takes two arguments: the name of the function (or the function itself), and the sequence (a list, a pandas series, etc.). So the map function in conjunction with the lambda function will map the function across all elements within the sequence.
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
sum_a_b = map(lambda x, y: x + y, a, b)
Output:
print(sum_a_b)
[6, 8, 10, 12]
The example map/lambda combo takes the function x + y, and the sequences a and b. It is treating the respective items from each sequence as the function’s x and y, respectively, to return the outcome.
Reduce()
This function will also take a list as an argument and then, you guessed it, reduce the list using a repetitive operation over the pairs of the list. We can find this function in the ‘functools’ module, which we must first import.
from functools import reduce
ex_list = [24, 5, 6, 45, 96, 123, 69]
difference = reduce(lambda x: y: x - y, ex_list)
Output:
print(difference)
-320
This function takes the difference of the first and second items in the list and then continues on until the final element of the list has been differenced. Similarly, it is taking the lambda function as the operation to perform, and the ex_list as the sequence to perform the function on.
Comentários