Python Lambda Functions, Filter, Map, Reduce

Lambda Functions

  • A lambda function is a small anonymous function.
  • A lambda function can take any number of arguments, but can only have one expression.
  • Syntax : lambda arguments : expression
>>> x = lambda a: a*2
>>> x(4)
8
>>> x = lambda a: a**2
>>> x(4)
16
>>> y = lambda a, b : a * b
>>> y(3,9)
27
>>> y = lambda a, b, c : a * b * c
>>> y(2,4,6)
48

Lambda Functions inside functions

>>> def multiplier(n):
...     return lambda a: a * n
...
>>> mul = multiplier(3)
>>> mul(4)
>>> 12

Filter function

Construct an iterator from those elements of iterable for which function returns true. filter(function, iterable)

>>> myList = [1,2,3,4,5,6,7,8,9]
>>> myFilter = list(filter(lambda x: (x%3==0), myList))
>>> myFilter
[3, 6, 9]

>>> dict = [{'name':'Girish','Age':25,'Course':'Python'},{'name':'Sanjay','Age':35,'Course':'Python'},{'name':'Ganesh','Age':20,'Course':'Java'},{'name':'Pranathi','Age':20,'Course':'Cordova'}]
>>> myFilter = filter(lambda x: x['Course']=='Python',dict)
>>> myFilter
<filter object at 0x033B3490>
>>> list(myFilter)
[{'name': 'Girish', 'Age': 25, 'Course': 'Python'}, {'name': 'Sanjay', 'Age': 35, 'Course': 'Python'}]

Map Function

Return an iterator that applies function to every item of iterable, yielding the results. map(function, iterable, …)

>>> myMap =list(map(lambda x: x**2, myList))
>>> myMap
[1, 4, 9, 16, 25, 36, 49, 64, 81]

>>> myMap =list(map(lambda x, y: x * y, myList, myList2))
>>> myMap
[9, 16, 21, 24, 25, 24, 21, 16, 9]

>>> dict = [{'name':'Girish','Age':25,'Course':'Python'},{'name':'Sanjay','Age':35,'Course':'Python'},{'name':'Ganesh','Age':20,'Course':'Java'},{'name':'Pranathi','Age':20,'Course':'Cordova'}]

>>> map(lambda x: x['Course'],dict)
<map object at 0x033B3410>
>>> list(map(lambda x: x['Course'],dict))
['Python', 'Python', 'Java', 'Cordova']

>>> list(map(lambda x: x['name'],dict))
['Girish', 'Sanjay', 'Ganesh', 'Pranathi']

Reduce Function

The functools module is for higher-order functions: functions that act on or return other functions. Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value.
functools.reduce(function, iterable[, initializer])

>>> from functools import reduce
>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15
>>> reduce(lambda x, y: x*y, [1, 2, 3, 4, 5])
120
>>> reduce(lambda x, y: x-y, [1, 2, 3, 4, 5])
-13
>>> reduce(lambda x, y: x/y, [1, 2, 3, 4, 5])
0.008333333333333333

The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.

References

  • https://docs.python.org/3/library/functions.html
  • https://docs.python.org/3/library/functools.html
  • https://docs.python.org/3/tutorial/controlflow.html

Learn more about Python features in our upcoming blog articles

Happy Learning!