## Lambda expressions in Python

2011/04/02 § 1 Comment

Python supports lambda expressions[1], having lambda support in a language is very nice feature, specially if you like functional style of syntax you will love it. Its not just a choice of style of programming sometimes lambda expressions helps to solve some problem quite elegantly, it helps to make code clean readable and free from unwanted functions which could have been easily replaced with lambda functions. Because of all these advantages i really love the lambda capability in python, here i will try to discuss some of the situations where lambda expressions could be helpful and explain the different style of writing lambda functions.

Though pythons lambda capability is not strong as many functional languages as it only offers lambdas which with expressions, not statements[4] but if we consider this feature carefully it is sufficient for most of the problems. Lambda functions doesn’t have name and they are called from where they are defined so they are also called anonymous functions, in python they are called by keyword **lambda **and their sysntatic structure looks like this *lambda arguments: expression*

so lets see how a simple lambda function look like

>>> func = lambda x : x*3 >>> func(3) 9

this is a very simple lambda function where given any value that value, it is multiplied by 3, so when 3 is passed in function *func *it returns 9. This demonstrate how a simple lambda function can be defined but this function might have very few places where it can find its use so lets see some other functions. One situations where lambda fist perfectly is in filters and maps, let say we have a list of numbers and we want to filter out only the odd numbers, for that we can do something like this

>>> print filter(lambda x : x%2 != 0, range(10)) [1, 3, 5, 7, 9]

here lambda provide condition to filter all unwanted numbers, similarly we can get similar use of lambda in map.

Up to now it looks simple but what if we need to give some condition on the lambda expression, our needs can’t always be that simple as shown in the examples above, say we have a lambda function that takes two values and if the first value is greater than second it does subtract first from second otherwise returns zero, we can express such case as follows

>>> lambda_check = lambda x, y : x - y if (x > y) else 0 >>> lambda_check(17, 7) 10 # or alternatively it can be written as >>> lambda_check = lambda x, y : (x > y and x - y ) or 0 >>> lambda_check(17, 7) 10

the syntax looks self explanatory, the alternative syntax looks bit odd but it depends on **and or** execution strategy. If-else condition works quite well, now lets look at more complex condition, let say we have 2 values if first value is bigger, second is subtracted from first, if second is bigger first is subtracted from second and if both are equal they are added if all fails returns zero. To express this condition we sould take the alternative suntax above and it can be expressed as follows

lambda_check = lambda x, y : (x > y and x - y) or \ (y > x and y - x) or \ (x == y and x + y) or 0 >>> lambda_check(17, 7) 10

This syntax looks bit complex but if we look carefully we just make the use of **and** and **or** to create this expression. We can use same techniques for different datatypes, conditions or even you can call functions.

if you want to read more about lambda expressions in python you can follow the links below.

[1] http://docs.python.org/reference/expressions.html#lambda

[2] http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/

[3] http://diveintopython.org/power_of_introspection/lambda_functions.html

[4] http://lambda-the-ultimate.org/node/1480

[...] http://dineshsimk.wordpress.com/2011/04/02/lambda-expressions-in-python/ [...]