Skip to content

Anonymous Functionss


  • A lambda function is a small anonymous function.
  • A lambda function can take any number of arguments, but can only have one expression.
x = lambda a : a + 10

x = lambda a, b, c : a + b + c
print(x(5, 6, 2))


map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.). We can pass as many iterable objects as we want after passing the function we want to use:

The syntax is: map(function, iterable(s))

def addition(n): 
    return n + n 

# We double all numbers using map() 
numbers = (1, 2, 3, 4) 
result = map(addition, numbers) 
>>>[2, 4, 6, 8]

# We can also use lambda expressions with map to achieve above result.
numbers = (1, 2, 3, 4) 
result = map(lambda x: x + x, numbers) 
>>>[2, 4, 6, 8]

# Add two lists using map and lambda 
numbers1 = [1, 2, 3] 
numbers2 = [4, 5, 6] 

result = map(lambda x, y: x + y, numbers1, numbers2) 
>>>[5, 7, 9]

# List of strings 
l = ['sat', 'bat', 'cat', 'mat'] 

# map() can listify the list of strings individually 
test = list(map(list, l)) 
>>>[['s', 'a', 't'], ['b', 'a', 't'], ['c', 'a', 't'], ['m', 'a', 't']]


Similar to map(), filter() takes a function object and an iterable and creates a new list.

As the name suggests, filter() forms a new list that contains only elements that satisfy a certain condition, i.e. the function we passed returns True.

The syntax is:

filter(function, iterable(s))

To create a new list that only contain elements for which the starts_with_A() function returns True:

# Without using lambdas
def starts_with_A(s):
    return s[0] == "A"

fruit = ["Apple", "Banana", "Pear", "Apricot", "Orange"]
filter_object = filter(starts_with_A, fruit)

>>>['Apple', 'Apricot']

# using a lambda:

fruit = ["Apple", "Banana", "Pear", "Apricot", "Orange"]
filter_object = filter(lambda s: s[0] == "A", fruit)

>>>['Apple', 'Apricot']


The reduce() function in Python takes in a function and a list as argument. reduce() works differently than map() and filter(). It does not return a new list based on the function and iterable we've passed. Instead, it returns a single value.

reduce() works by calling the function we passed for the first two items in the sequence. The result returned by the function is used in another call to function alongside with the next (third in this case), element.

reduce() is a bit harder to understand than map() and filter(), so let's look at a step by step example:

We start with a list [2, 4, 7, 3] and pass the add(x, y) function to reduce() alongside this list, without an initial value

reduce() calls add(2, 4), and add() returns 6

reduce() calls add(6, 7) (result of the previous call to add() and the next element in the list as parameters), and add() returns 13

reduce() calls add(13, 3), and add() returns 16

Since no more elements are left in the sequence, reduce() returns 16

The only difference, if we had given an initial value would have been an additional step - 1.5. where reduce() would call add(initial, 2) and use that return value in step 2.

Let's go ahead and use the reduce() function:

from functools import reduce

def add(x, y):
    return x + y

list = [2, 4, 7, 3]
print(reduce(add, list))
>>> 16
Again, this could be written using lambdas:
from functools import reduce

list = [2, 4, 7, 3]
print(reduce(lambda x, y: x + y, list))
>>> 16

print("With an initial value: " + str(reduce(lambda x, y: x + y, list, 10)))
And the code would result in:
>>> With an initial value: 26