Python Concepts/lambda, map(), filter(), reduce()

From Wikiversity
Jump to navigation Jump to search

Objective[edit | edit source]

  • To understand lambda functions, map() , filter(), reduce() functions

lambda[edit | edit source]

Lambda is an one-line function with any number of arguments.

 add = lambda a, b, c: a + b + c

Before the colon (:) a, b, c are the parameters. After the colon is the return value.

Example 1

 add = lambda a, b, c: a + b + c
 print("The return value from add() is ", add(1, 2, 3))
 [Console]
 
 The return value from add() is 6
 
 [Program Finished]

The return value is the sum of all the parameters in add() lambda function.

Example 2

 add_one = lambda *x: [y+1 for y in x]
 print(add_one(2, 6, 7, 5, 3))
 [Console]
 
 [3, 7, 8, 6, 4]
 
 [Program finished]
  • x is a tuple with any number of arguments. In return there is a list comprehension: Each value in x will be sum by 1, resulting in the return of a list all the given integers added by 1.

Example 3

 square = lambda *x: [y*y for y in x]
 print(square(2, 6, 7, 5, 3))
 [Console]
 
 [4, 36, 49, 25, 9]
 
 [Program finished]

Return of a list all the given integers multiplied by themselves (square of a number)

  • 2 × 2 = 4
  • 6 × 6 = 36
  • 7 × 7 = 49
  • 5 × 5 = 25
  • 3 × 3 = 9

map()[edit | edit source]

The map(function, iterable). The function in the parameter interacts with the list and then a map is returned.

Example 1

 numbers = [-3, 4, -6, -9, 0, 7, 1]
 
 def square(x):
   return x * x
 
 squared_numbers = list(map(square, numbers))
 # Transform map object to list
 print(squared_numbers)
 [Console]
 
 [9, 16, 36, 81, 0, 49, 1]
 
 [Program finished]

The square() function allows one parameter. map() can use this function for every value in a list.

Example 2

 numbers = [-3, 4, -6, -9, 0, 7, 1]
 
 minus_one_numbers = list(map(lambda x: x - 1, numbers))
 # Transform map object to list
 
 print(minus_one_numbers)
 [Console]
 
 [-4, 3, -7, -10, -1, 6, 0]
 
 [Program finished]

This lambda function as the first parameter of map() has a single parameter. map() passes every value from the list through the function.

filter()[edit | edit source]

filter(function, iterable). This function returns a filter object which has numbers that passed by a condition defined by a function.

Example 1

 def less_than_zero(x):
   return x < 0 # True or False
 numbers = [6, -4, -2, 8, -2, 5, 6, 9]
 # Convert filter object to list
 print(list(filter(less_than_zero, numbers)))
 [Console]
 
 [-4, -2, -2]
 
 [Finished program]

Each number of the variable 'numbers' passes through less_than_zero(). If it's true, the number passed through the filter().

Example 2

 def username_limit_len(username):
   return len(username) < 15 and len(username) > 3 # True or False
 
 usernames = ["normal_user", "Aa", "user2839862", "1234isntagoodpassword", "verified_user", ".", "ejekwbwuqkabwjwkanxg"]
 
 # Convert filter object to list
 print(list(filter(username_limit_len, usernames)))
 [Console]
 
 ["normal_user", "user2839862", "verified_user"]

Each item in 'usernames' is filtered. If its length is greater than 3 characters and its length is less than 15 characters it's on the printed list above.

functools.reduce()[edit | edit source]

reduce(function, iterable). It needs to be imported: 'from functools import reduce'

  • The function in the first parameter needs to have two arguments.
  • The list or tuple needs to have at least 2 items.
  • It takes the first and second items of 'numbers', then if there's another item, the return result with the third (next) item go into the reduce()'s function argument...
 from functools import reduce
 
 numbers = [5, 8, -4, -2, 5, 3, -1]
 sum = reduce(lambda a, b: a + b, numbers)
 print("The result is ", sum)
 [Console]
 
 The result is 14
 
 [Program finished]

What happened

  • numbers = [5, 8, -4, -2, 5, 3, -1]
  • lambda a, b: a + b
  • reduce took the two first items in 'numbers' and put them in the lambda function
  • lambda 5, 8 -> 5 + 8
  • 5 + 8 == 13

the return of lambda's function is placed with the third item in lambda function again...

  • lambda 13, -4 -> 13 + -4 == 9
  • lambda 9, -2 -> 9 + -2 == 7
  • lambda 7, 5 -> 12
  • lambda 12, 3 -> 15
  • lambda 15, -1 -> 14
  • There is no more items in 'numbers' the reduce()'s return is 14.