A Guide to List Comprehensions, reduce() and lambda-expressions

List Comprehensions

reduce() 
and 
lambda-expressions

A "list comprehension" is an expression that constructs a new list using the elements from another list.

Its simplest form is:
[ expression for k in some-list ]
This will step through the list, obtain each element (k) and construct a new list using some expression involving that element. This is a replacement (or alternative) to the map() function in Python and many other languages.

A somewhat more complicated list comprehension has an addition which can select which expressions will end up in the new list, and which will not.  This is a replacement (or alternative) to the filter() function in Python and many other languages.
[ expression for k in list if true-false-expression ]

Here are some examples:

>>> p = [3, -1, 12, 5, -2]
>>> q = [ k+1 for k in p ]  # add 1 to each element
>>> print q
[4, 0, 13, 6, -1]
>>> print p
[3, -1, 12, 5, -2]  # p hasn't changed

>>> def double(n): 
       return 2*n
>>> q = [ double(k) for k in p ]  # apply a function to k
>>> print q
[6, -2, 24, 10, -4]

>>> q = [ k%2 for k in p ]  # 1 if odd, 0 if even
>>> print q
[1, 1, 0, 1, 0]

>>> q = [ k for k in p if k > 0 ]  # selects positive ones
>>> print q
[3, 12, 5]

 

reduce(calc-function, list) is a function which applies the calc-function to the elements of a list to end up reducing the list to a resultant single value.  For instance, reduce() can apply the addition-function to calculate the sum of the elements of a list.

The calc-function that you give to reduce() must take two arguments and return a value.  reduce() will give the first 2 elements of the list (the second argument) to calc-function and calc-function will return a result (we'll call it r).  reduce() will then give r and the third element of the list to calc-function and assign the result back to rreduce() will give r and the 4th element to calc-function and assign the result back to r.  It will continue to do this until it runs out of elements of the list.  It returns r.

Examples:

>>> def add(x,y):
       return x+y
>>> p = [4, 1, 3, 2]
>>> print reduce(add,p)
10

Lambda-expressions: This is a mechanism of creating an "anonymous" (or un-named) function.  Notice that reduce() expects a function as its first argument.  In the example above, we only needed to define the add() function so that we could pass it on to reduce().  But with a lambda-expression, we can define the equivalent of the add() function directly inside first the argument to reduce() (and never have to give it a name).  Here a simple form of a lambda-expression that adds two numbers together:

lambda x,y: x+y

We can then place this into the first argument to reduce():

>>> print reduce(lambda x,y: x+y,p)
10

if-else expressions: This is an expression of the form:

  expression-1 if true-false-test else expression-2

It returns expression-1 if the true-false-test is True, otherwise it returns expression-2.  For instance:

>>> p = 5
>>> print 45 if p > 0 else 28
45
>>> a = 'even' if p%2 == 0 else 'odd'
>>> print a
odd

We can use all 3 constructs above (reduce(), lambda-expression and if-else) to calculate and print the largest element of a list:

>>> p = [4, 7, 2, 1, 6]
>>> print reduce(lambda x,y: x if x>y else y, p)
7