## Intermediate Python Knowledge

# Lambda Functions Aren’t Always Your Best Choices

## Don’t reinvent the wheels

Lambda functions, or simply lambdas, are anonymous functions in Python. They have the following general syntax. We use the `lambda`

keyword to indicate that we’re declaring a lambda function. We then specify the list of arguments, which can be zero or multiple. Then, we specify the expression after the colon.

`lambda arguments: expression`

Apparently, lambda functions are concise and can be used in various scenarios where an inline function is needed, such as being set as the `key`

argument in the `sorted()`

function (more on this later). However, once it’s learned, some beginners tend to overuse them even when there are more applicable built-in functions or other more neat solutions.

## Operations Over Sequences

It’s a common task that we need to calculate the sum of the numbers in a sequence. Suppose that we have a math contest with each team having three members. Here are the records of their scores.

`>>> `**# Create a list of tuples for sorting**

>>> scores = [(95, 98, 99), (94, 97, 93), (95, 92, 93), (100, 95, 98)]

We want to sort the list of tuples using the total score for each team. If we use a lambda function, it’ll be something like below.

`>>> `**# Sort the list with a lambda function**

>>> **# The most tedious**

>>> sorted(scores, key=lambda x: x[0] + x[1] + x[2])

[(95, 92, 93), (94, 97, 93), (95, 98, 99), (100, 95, 98)]

>>>

>>> **# Slightly better**

>>> sorted(scores, key=lambda x: sum(x))

[(95, 92, 93), (94, 97, 93), (95, 98, 99), (100, 95, 98)]

How about using the `sum()`

function directly? Much more neat, isn’t it?

`>>> `**# Best, using sum() directly**

>>> sorted(scores, key=sum)

[(95, 92, 93), (94, 97, 93), (95, 98, 99), (100, 95, 98)]

Using the same list of tuples, suppose that we want to find out the team whose member has the highest score. Instead of using the `sorted()`

function, we can use the built-in `max()`

function. Similarly, we can have two solutions with and without using a lambda function. See below.

`>>> `**# Find out the team with the member having the highest score**

>>> **# Using lambda**

>>> max(scores, key=lambda x: max(x))

(100, 95, 98)

>>>

>>> **# Using built-in directly**

>>> max(scores, key=max)

(100, 95, 98)

Besides the `sum()`

and `max()`

functions, there are several other functions, such as `len()`

and `min()`

, which you can consider as alternatives to lambda functions where applicable.

## Higher-Order Functions

We all probably know that functions are objects in Python. One implication is that functions can be used as arguments in other functions, and those functions that take other functions are termed higher-order functions, such as the built-in `map()`

function.

The `map()`

function has the following syntax: `map(func, iterables)`

. Basically, it takes a mapping function and applies the function to one or multiple iterables that are passed into the function. Notably, the number of iterables should match the number of needed arguments in the mapping function.

The `map()`

function’s return data type is a *map* object, which is an iterable, such that we can use it to create a *list* object using the `list()`

constructor. Let’s see an example of how we can use the `map()`

function together with a lambda function to create a *list* object.

`>>> `**# Create a list of numbers**

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

>>>

>>> **# Use the lambda with map()**

>>> squares0 = list(map(lambda x: x*x, numbers))

>>> squares0

[1, 4, 9, 16, 25]

However, we can actually achieve this functionality without using a lambda function. Instead, we can simply use the list comprehension technique, which is a much more concise way to create a *list* object. It has the following basic syntax: `[expression for item in iterable]`

. Let’s see how it works in action.

`>>> `**# Use the list comphrension**

>>> squares1 = [x*x for x in numbers]

>>> squares1

[1, 4, 9, 16, 25]

It’s also possible to apply some conditions to the list comprehension such that we’ll only include those that we’re interested in.

`>>> `**# List comprehension with conditional**

>>> odds_squares = [x*x for x in numbers if x%2]

>>> odds_squares

[1, 9, 25]

Besides the `map()`

function, there are a few other high order functions, including `filter()`

and `reduce()`

, which are commonly used with the lambda function. In many cases where you’re creating *list* objects, you can consider using list comprehension as a concise alternative.

## Conclusions

In this article, we reviewed two common scenarios where lambda functions are not recommended. So the take-away is that lambda functions are useful, but use them wisely.

Thanks for reading this article. Here are a few articles that are relevant to the current discussion.