# Python lambda

We can create anonymous functions, known as lambda functions. Lambda functions are different from normal Python functions, they origin from Lambda Calculus. It allows you to write very short functions.

Related Courses:

Lambda function example
This code shows the use of a lambda function:

```#!/usr/bin/env python   f = lambda x : 2 * x print f(3)```

A return statements is never used in a lambda function, it always returns
something. A lambda functions may contain if statements:

```#!/usr/bin/env python   f = lambda x: x > 10 print(f(2)) print(f(12))```

## map function

The definition of map is map(function,iterable). It applies a function to every item in the iteratable.  We can use map() to on a lambda function with a list:

```#!/usr/bin/env python   list = [1,2,3,4,5] squaredList = map(lambda x: x*x, list) print(squaredList)```

Anywhere you use lambda functions, you could use normal functions instead.  A lambda function is not a statement, it is an expression. Lambda functions do not support a block of statements.

## filter function

filter(function,iterable) creates a new list from the elmeents for which the function returns True. Example:

```#!/usr/bin/env python   list = [1,2,3,4,5,6,7,8,9,10] newList = filter(lambda x: x % 2 == 0, list) print(newList)```

The returning list returns contains only the elements for which the lambda expression “lamba x: x % 2 == 0”  is true.

## reduce function

The reduce function, reduce(function, iterable) applies two arguments cumulatively to the items of iterable, from left to right. Example:

```#!/usr/bin/env python   list = [1,2,3,4,5] s = reduce(lambda x,y: x+y, list) print(s)```

In this case the expression is always true, thus it simply sums up the elements of the list. Another example:

```#!/usr/bin/env python   list = [10,6,7,5,2,1,8,5] s = reduce(lambda x,y: x if (x > y) else y, list) print(s)```

## 8 thoughts on “Python lambda”

1. - June 16, 2017

There is an > somehow in there and it took me a bit to realize… oh this is just HTML showing. Whoever maintains this page may want to look into that. 🙂

1. - June 16, 2017

ugh… there is an & g t ; ^ it auto created the HTML.

1. - June 17, 2017

Thanks Trae! I’ve updated the article.

2. - August 22, 2015

The following comments apply to python 3.4.3; I did not try anything with v 2.x.
As Guest, Orri, points out: one must cast the return of the map function to a list, before one can print the output list. Therefore it is not a good idea to use “list” as a variable name, as it overwrites the list casting function. An alternative to Orri’s resolution is:

```List1 = [1,2,3,4,5] squaredList = list(map(lambda x: x*x, List1)) print(squaredList)```

A similar comment applies to the filter example and the following code fixes the problem:

```List2 = [1,2,3,4,5,6,7,8,9,10] newList = list(filter(lambda x: x % 2 == 0, List2)) print(newList)```

Finally, the function “reduce” does not seem to automatically available. One should at least import it; e.g.,

`from functools import reduce`

One doesn’t need to cast the result of applying reduce — at not in this example — since the result is a number which can readily be printed. Are there some other (interesting) examples involving “reduce”, where one would have to apply a cast?

3. - August 21, 2015

For the map function, I ended up doing the following since the map function returns the map object, not the content of the list.

```#!/usr/bin/env python   numbers = [1,2,3,4,5] squaredList = map(lambda x: x*x, numbers)   print(numbers) print(list(squaredList))```
4. - August 20, 2015

printing list objects like print(newList) brings a .
I have to rather do:
for item in newList:
print(item)

1. - November 23, 2015

What is the output of the last example ? I run it and got 10 !
I did not understand why ?

1. - November 23, 2015

Correct, the output of the last example is 10. The program applies a comparison to every x and y from left to right.
The statement ‘x if (x > y) else y’ is applied to every pair until no further reduction is possible. For example, if you have the list [1,3,10,4,1]
it will return 10 because at some point it will compare ‘x=3, y=10: if (3 > 10) return 3 else return 10’. The program keeps repeating the statement ‘x if (x > y) else y’ until it cannot be applied further.