# Using Lambda Functions in Python λ

# What are lambda functions?

In Python, lambda functions are a syntax that allows the use of anonymous functions in ones code.

Lambda functions take any number of inputs, perform some computation on these inputs, and return the result of this computation.

To best illustrate the uses of lambda functions, let’s first examine a normal function which takes in two numbers, `x`

and `y`

, multiplies them, and returns the result.

```
# Function that multiplies two numbers
def multiplyTwoNumbers(x, y):
return x*y
```

Now let’s see how this looks as a lambda function:

```
>> g = lambda x, y: x*y
>> print(g(6, 3))
>> 18
```

- Lambda functions contain an implicit return call
- Lambda functions contain only one expression

# Why use lambda functions?

Lambda functions work best when a computation needs to be done but does not necessitate a separate function.

In our last example we only saved one line of code. Let’s showcase the true strengths of anonymous functions.

For our example, say we need to find the value of `x % 10`

for every element `x`

in a list. Using traditional code, it may look something like this.

```
# This code preforms modulus 10 on every element in a list
def getMod(x):
return(x%10)
myList = [44, 12, 98, 34, 17]
newList = []
for i in myList:
newList[i] = getMod(i)
print(newList)
```

This gives us `[4, 2, 8, 4, 7]`

. Let’s clean this up with a lambda expression.

```
# This code preforms modulus 10 on every element in a list
myList = [44, 12, 98, 34, 17]
newList = list(map(lambda x: x%10, myList))
print(newList)
```

The result is `[4, 2, 8, 4, 7]`

, just as expected.

- The
`map`

function takes two inputs: A function, and a list of arguments to pass to it. `map`

returns a`map object`

, so we cast it to a list in order to read it.- Lambda functions can declared inline and thrown away. No declaration or variable assignment required.

# Lambda functions and `filter()`

Here is an example of the `filter()`

function in python. `filter()`

takes in two arguments:

- A function object, (in this case a lambda function) which returns a boolean
- Iterable list of aguments

`filter()`

works similarly to `map()`

except only the values for which our lambda function returns `True`

are added to the list

Let’s say we wanted to filter out numbers that are *not* a multiple of three.

```
myList = [44, 12, 98, 34, 17]
newList =list(filter(lambda x: x%3 == 0, myList))
print(newList)
```

This returns `[12]`

, which makes sense because it is the only multiple of three in our list. This is certainly better than a for loop with a traditionally defined function!

# Lambda functions and `reduce()`

The reduce function is used when one wishes to perform a rolling computation on an iterable. For example, let’s say you wanted to find the product of numbers in a list. Using traditional python, it might look something like this:

```
# code to find the product of all elements in a list
myList = [2, 3, 4, 5, 6]
total = 1
for i in myList:
total = total*i
print(total)
```

The output here is `720`

.

The same process using a lambda expression and `reduce()`

looks something like

```
from functools import reduce
myList = [2, 3, 4, 5, 6]
print(reduce(lambda x,y: x*y , myList))
```

Again, the output is `720`

.

Basically, the reduce function is performing the following operation:

$$ ((((2 * 3) * 4 ) * 5) * 6) $$

Pretty nifty, huh? 😄

# Conclusion

Lambda functions. Used by CS students trying to flex on their classmates? Indeed. Valuable and useful tool to optimize quick calculations? Hell yes.

We learned how to leverage lambda expressions with the `map()`

, `reduce()`

, and `filter()`

functions to make quick data processing a breeze. These are some pretty trivial examples but the concept can be extrapolated to fit real world applications.