syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Python what is lambda?

A lambda is like a function. It takes an input, works with the input, and returns an output.

Usually a lambda is contained within a short line of syntax.

Often a function will span many lines.

When I create a lambda I often give it a name.

When I call that name it behaves, to my eyes, exactly like a function.

I captured a few lines of lambda related syntax in my Python interpreter. Next, I annotated them. Finally, I pasted them below for you to read:
dan@feb ~/spark $ 
dan@feb ~/spark $ 
dan@feb ~/spark $ python
Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Aug 21 2014, 18:22:21) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> 
>>> 

I start by creating a lambda with no name:

>>> 
>>> 
>>> lambda aline: 'hello'
<function <lambda> at 0x7f3185a71d70>
>>> 
>>> 

Python just told me that my unnamed (anonymous) lambda is a function.

>>> 
>>> mylam = lambda aline: 'hello'
>>> 

I just gave it a name. Does it act like a function now?

>>> 
>>> mylam(1.1)
'hello'
>>> 
>>> mylam([1,2,3])
'hello'
>>> 
>>> mylam('hey')
'hello'
>>> 
>>> 

Yes, mylam acts like a function.

>>> 
>>> 

A lambda by itself is not interesting.
But when I combine it with a Python method named map(),
I can use that combo to operate on a Python list
without my dependence on a loop:

>>> 
>>> 
>>> mylist = [1,2,3]
>>> 
>>> map(mylam, mylist)
['hello', 'hello', 'hello']
>>> 
>>> urlam = lambda nmbr: nmbr * nmbr
>>> 
>>> urlam(4)
16
>>> 
>>> map(urlam, mylist)
[1, 4, 9]
>>> 
>>> 

I noticed that if-then syntax I need inside a lambda is different than what I normally write:

>>> 
>>> 
>>> theirlam = lambda nmbr: (nmbr * nmbr) if (nmbr == 1 or nmbr == 2) else nmbr
>>> 
>>> theirlam(2)
4
>>> 
>>> theirlam(3)
3
>>> 
>>> map(theirlam, mylist)
[1, 4, 3]
>>> 
>>> 

Writing a lambda which accepts two variables is easy:

>>> 
>>> twovar_lam = lambda v1,v2: v1 + v2
>>> 
>>> twovar_lam(2,3)
5
>>> 
>>> 

I was not able to use it inside of map though:

>>> 
>>> 
>>> listo2vars = [1,1,2,2,3,3]
>>> 
>>> map(twovar_lam, listo2vars)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 2 arguments (1 given)
>>> 
>>> 
>>> map(twovar_lam, [a,b for a,b in listo2vars])
  File "<stdin>", line 1
    map(twovar_lam, [a,b for a,b in listo2vars])
                           ^
SyntaxError: invalid syntax
>>> 
>>> 

Next I looked at a Python method named reduce().

This method takes a collection, which is usually a Python list,
and reduces it down to one value.

How do I call reduce()?

Here is the signature:

my1value = reduce(reduction_logic, python_list)

Obviously I need to supply python_list.
It might look like this: python_list = [1,2,3]
That is the easy part.

Second, I need to supply reduction_logic.

Python asks that I supply reduction_logic in the form of a lambda.

Also Python asks that the lambda accept two values and return one value:

reduction_logic = lambda a,b: do_logic_here_which_returns c

Here is an example of a (useless) lambda which accepts two floats and returns one float:

reduction_logic = lambda a,b: (a + b) / 2

Here is a lambda which is useful:

reduction_logic = lambda a,b: (a + b)

So that works great if my collection has only two objects.

Suppose though, that I have a collection of 7 objects.

The author of reduce() responds this way:

Do not worry, I will handle any number of objects.
You only need to supply enough logic to reduce two objects into one object.

Why can he/she say this?

My assumption is that reduce() uses a loop.

The loop reads the first two objects in the collection and reduces them to one object
(using my two-value-lambda).

Then it reduces that one object with the third object into one object.

Then it reduces that one object with the fourth object into one object.

Eventually we reduce the collection into one object, two objects at a time.

Here is a demo of me calling reduce:

>>> 
>>> reduction_logic = lambda a,b: (a + b)
>>> python_list = [1,2,3]
>>> 
>>> 
>>> reduce(reduction_logic, python_list)
6
>>> 
>>> 

Next, I demonstrate an idea called map-reduce:

>>> 
>>> map_logic   = lambda input: (input * input)
>>> python_list = [1,2,3]
>>> list_from_map = map(map_logic, python_list)
>>> list_from_map
[1, 4, 9]
>>> 
>>> reduce(reduction_logic, list_from_map)
14
>>> 
>>> 

In English, I used map() to square each number in python_list.
Then I used reduce() to sum the squares.

>>> 
>>> 
>>> quit()
dan@feb ~/spark $ 
dan@feb ~/spark $ 


Map-Reduce is an important idea when dealing with big data calculations.

We can use Python-lambda to understand how to use the idea.


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me