Python Lambda Functions

Dmitriy Korostelev
4 min readJul 31, 2021

--

Introduction

The lambda keyword in Python provides a short form to declare small anonymous functions. Lambda functions behave just like regular functions declared with the def keyword. For example, let’s look at the definition of a simple lambda function that performs multiplication:

The same mul function can be defined with the keyword def:

The syntax of the lambda function is:

(lambda comma, separated, arguments : an expression using arguments)

A lambda function can only have one expression in its body. Note that we are not talking about a statement. A statement is a line of code that does something but does not generate value. For example, an if instruction or a for loop are examples of instructions. Expressions, on the other hand, are values. You can’t replace a statement with a value. But you can easily replace all expressions in a program with values, and the program will continue to work correctly.

For example:

  • 3 * 15 the expression with a value of 15.
  • 3 < 15 the expression with a value of True.
  • True or (15 < 3) the expression with a value of True.

The body of the lambda function must be an expression because its value will be what the implicit return will produce.

Let's consider the following example:

So what happened here? We use lambda to define a function mul with two arguments and then immediately call it with arguments 5 and 3. The main difference here is that we didn’t have to associate an object-function with a name before using it, as we do when creating an ordinary function. We simply formulated the expression we wanted to compute as part of a lambda, and then immediately computed it by calling the lambda expression as an ordinary function. We could, of course, first associate the lambda function with a variable, as was shown earlier:

Examples

So, when should you use lambda functions in your program code? Technically, you can apply a lambda expression in any situation where you are expected to provide a function object. And because lambda functions can be anonymous, you don’t even need to assign them a name before doing so.

Sorted

Sorting the list using the built-in sorted function is an excellent example of using a lambda function.

The syntax for the sorted function is:

sorted(iterable, key, reverse)

Let’s look at the example:

The sorted function returns a new sorted list, which is obtained from the iterated object that was passed as an argument. With the key parameter, you can specify exactly how to perform the sorting. The key parameter expects a function with which to perform the comparison. In this example, we passed the lambda function lambda x: x ** 2 as the comparison function. If the reversed parameter is set to true, then the iterable would be sorted in reverse (descending) order, by default it is set as false.

Map

map(function, iterable)

The map iterates through all items in the given iterable and execute the function we passed as an argument on each of them. As a result, the function returns an iterator with the results:

Here we used the lambda function again, think about what happened here ;).

Reduce

reduce(function, sequence[, initial])

The reduce works differently than map. It does not return a new list based on the function and iterable we’ve passed. Instead, it returns a single value.
The reduce function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along. Also, in Python 3 reduce isn’t a built-in function anymore, and it can be found in the functools module.

Here is an example with reduce function performing summation using lambda:

Filter

filter(function, iterable(s))

Similar to map, filter takes a function object and an iterable and creates a new iterable. As the name suggests, filter forms a new list that contains only elements that satisfy a certain condition, i.e. the function we passed returns True.

In this example, we filter values from the range [-5, 5] to get only positive values:

Conclusion

So, lambda functions are handy when you need to calculate a single simple expression, but if you notice that you’re getting anything even distantly complex with lambda expressions, you’d better look up the definition of a standalone function with the right name. Saving a few keystrokes won’t make a difference in the long run, but your colleagues (and yourself, when rereading your old code) will appreciate clean and readable code more than terse witchcraft.

--

--