What are lambda functions in Python?

  • How are Lambda functions different from standard functions?
  • Where did lambda functions come from?
  • What are the different ways in which lambda functions can take arguments?
  • How can lambda functions be combined with built-in functions like filter() and map() in Python?

In Python, Lambda functions or anonymous functions are functions that are defined without a name but can be assigned a variable name. Lambda functions in Python and other programming languages have their roots in Lambda Calculus.

The standard functions in Python are defined using the def keyword whereas the anonymous/lambda functions are defined using the lambda keyword.

The syntax for Lambda functions is as follows:

lambda arguments: expression

These functions can take any number of arguments but evaluates and returns only one expression without using the return keyword.

The following should be kept in mind while using lambda functions in Python:

  • It can only contain expressions and can’t include statements. Statements like return, pass, assert, or raise will raise a SyntaxError in Python.
  • It is written as a single line of code and can be immediately invoked.
  • It is essentially an expression, that is why it can be named or not.

Let us understand with the help of an example. We will look at both the standard function and Lambda function to add five to a given number which have the same functionality.

# the standard function
>>> def add_5(x):
...     return x + 5
# the lambda function
>>> lambda x: x + 5

One can apply the above Lambda function to an argument by surrounding the function and its argument with parentheses:

>>> (lambda x: x + 5)(10)

One can also assign the lambda function to a variable and call it later as follows:

>>> add_5 = lambda x: x + 5
>>> add_5(10)

The following are few ways one can pass arguments into a lambda function:

>>> (lambda a, b, c: a + b + c)(1, 2, 3)
>>> (lambda a, b, c=3: a + b + c)(1, 2)
>>> (lambda a, b, c=3: a + b + c)(b=2, a=1)
>>> (lambda *args: sum(args))(1, 2, 3)
>>> (lambda **kwargs: sum(kwargs.values()))(a=1, b=2, c=3)
>>> (lambda a, b=0, c=0: a + b + c)(1, b=2, c=3)

Often times a Lambda function is passed as an argument to another function. Few of those cases are when one uses Lambda functions with built-in functions in Python such as filter(), map() etc.

Using Lambda functions with map()

The built-in function map() takes a function as a first argument and applies it to each of the elements of its second argument, an iterable such as strings, lists and tuples.

Let us look at a couple of examples:

>>> list(map(lambda x: x.lower(), ['FOO', 'BAR', 'BAZ']))
['foo', 'bar', 'baz']
>>> list(map(lambda x: x**2, [1, 2, 3, 4]))
[1, 4, 9, 16]

Using Lambda functions with filter()

The built-in function filter() takes in a function as its first argument and an iterable as its second argument. This offers a way to filter out all the elements of iterable, for which the function returns True.

Let us look at a couple of examples:

>>> list(filter(lambda x: ('a' in x) , ['foo', 'bar', 'baz']))
['bar', 'baz']
>>> list(filter(lambda x: (x%2 == 0), [1, 2, 3, 4]))
[2, 4]

The main use case of a Lambda function is that, that it should be invoked when one requires a function for a short period of time.

1 Like