Some tricks usage of lambda functions in Python you might want to know

Lambda functions are often used as anonymous functions. They can easily be passed as a parameter of another defined function (with signature) in order to be exploited. I would like to present you with a few tricks that I have discovered by using these lambda functions.

1. Common usages

A rather classical use of lambda functions is to apply it to a so-called iterable element through the map() function. This makes it possible, for example, to apply our anonymous function to each item in a list.

Typical usage of lambda anonymous function with map

Output is:

[1, 4, 9, 16, 25]

One thing that can be added here is that the anonymous function created can be retrieved via the symbol `_` which points to the last instruction of the Python interpreter.

Output is:

27

So let’s get down to the facts about the things to watch out for when using the lambda functions.

2. Passing global parameters

Here we suggest to declare our lambda function and store it in f. We also use a previously declared variable within our lambda function instruction. Let’s have a look at the result.

Using global context variable

Outputs are:

30
40

It may be just a small aspect, but it can be very important. Indeed, the fact of exploiting a global variable (of a greater scope than the lambda function) within our lambda function, it will take into consideration, if existing, of its new value.

Another example to better understand what side-effects we may be confronted with is the following one:

Lambda functions using loop variable

Output is:

[9, 10, 11, 12, 13, 14, 15, 16, 17, 18]

Here, we first define a list of anonymous lambda functions lambda_functions. Its instruction is simple, it adds the value of i to the variable passed in parameter x.

The second instruction will call each of the 10 functions defined in the list (via access to the j index), and pass the j parameter, the current value of the loop. The outcome is then stored in the variable lambda_results.

Let’s pay attention now to the obtained results. This is a list of 10 values ranging from 9 to 18. Why a value of 9 for the first item? Simply because in the global context and at the time of the call to the lambda function, the variable i has the value of 9 (last value of the loop of the previous instruction).

These are side effects which source must be known because they can quickly impact the expected results.

3. Use of local defined parameter

One way to counteract this is to simply use the so-called partial anonymous functions. This is where all the beauty of lambda functions makes sense.

Let’s take our previous example again, but this time with a small modification that will allow us to obtain the expected results:

Output is:

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

The result is now indeed the expected one! The sum of the elements two by two (i and j) for each of the anonymous functions. The trick is to set a second default local parameter to the lambda function, which is here also called i. Thanks to the known process of ‘‘variable shadowing’’, it allows to get back the real value of i expected. It is typically for the example that the second parameter is also called i, but of course, it could have any name as long as it includes the value of i at the desired time.

4. Create a simple generator

Knowing this principle, it is possible for us to declare anonymous functions which can be called without providing any parameter. This can, in some cases, be interesting.

Let’s define, for example, a uniform sampler function with default values:

Output is:

[0.02623335 0.43817397 0.58111607 0.01777551 0.42089516]

This is a very simple example, but many other cases may exist. Such as in the context of processing within a loop, which would allow to obtain specific lambda functions composed of default parameters.

Conclusion

Anonymous lambda functions are widely used today, especially for applications with iterable elements. Here have been presented some cases where the scope of the variables can impact the expected behaviour. The strength of the partial application of parameters to our anonymous functions is very interesting to thwart these problems.

This article is very short, but I wanted to tackle this topic. I hope you enjoyed it! :)

PhD Student in Machine Learning and Computer Graphics

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store