Python Lambda Functions

Python supports manipulation of anonymous functions (i.e. lambda expressions). Lambda functions is a way to create functions without names. These functions are throwaway functions and are used where they are created. These anonymous functions are handy to use in a places where you can’t do function declaration.

  • Lambda lets you define and use a function inside an if statement, or inside a python list.
  • Lambda expressions can be used in a places where you can’t use function declaration.
  • Lambda expressions, are just an expressions.
  • Statements are not allowed inside the lambda functions (or expressions).
  • Lambda functions are used in combinations with functions like : filter(), map() and reduce().

Code in this tutorial is tested on python 2.6 and python 3.2. Only difference is of reduce() function which was removed in python 3.x release, otherwise rest of the code in the tutorial works just fine.

Syntax:

lambda argument_list: expression

The argument list is comma separated value and the expression list consist of arithmetic expression using these argument lists. You can then assign this function to a variable and then use this variable for further manipulation or output.

Let’s take a look at example, here x is the argument and it is squared in this lamda function. The value of lambda function is then returned to the square_var.

squar_var=lambda x:x**2
print(square_var(8))

The above code is tested on python 3.2 (linux). In case of older version you can make appropriate changes in the print statement. As per the new change in python 3 we’re using print() instead of print “”.

Now let’s take a look at the three functions.

Map() Function

This function takes two arguments. The first argument in the function is the name of function and the second is the sequence or the list.

Syntax: map(func, seq)

Let’s take a look at map() function with example of a list.

a=[1,2,3,4]
b=[2,3,4,5]
c=map(lambda x,y:x+y, a,b)
print(list(c))

Output: [3, 6, 9, 12]

Note: If you don’t print it as a list(c) then chances are there that output will be printed as a map object. So you have to print it as a list.

Like the above example you can also evaluate some more mathematical expressions and check the list as a boolean output. For example, make a list with number divisible and non-divisible by 2. Now add that condition in the lambda function expression and then get the output as a list. You’ll find that it prints true and false output.

Filter() Function

Like map() function this filter() function also takes two arguments – func and list. Let’s take an example to see how the filter() function works.

b=[2,3,4,9]
c=filter(lambda x:x%3==0,b)
print(list(c))

Output: 3,9

You see filter() function removes the list items that are not divisible by 3. It only displays the two items that qualifies for the same. So the list argument in the filter function only returns value that are true as per the condition.

Reduce() function

Reduce function is not available in python 3.x. You can skip this section if you’re using the new version of python. In case if you wish to use this function, you can use functools.reduce() instead. Reduce() function takes two arguments and the seq/list item continually evaluates until the last item. This way the list/sequence is reduced as per the arithmetical expression condition.

Let’s take a look at example to see how it works.

a=[1,2,3,4]
reduce(lambda x,y:x>y, a)
Output: false

This sums up our small tutorial on python lambda functions. You can keep tab on python 3.x version updates to see changes to the lambda functions. Hope this information helps. Feel free to let me know your views in the comments. You can also send me tweet @maheshkale, if you have any queries or feedback.