Data Warehousing, BI and Data Science

2 December 2020

Python: Function, Lambda, Map, Filter, Reduce

Filed under: Data Warehousing — Vincent Rainardi @ 6:43 am

A function with 2 arguments:

def f(a,b):
    c = a * b
    return c
Output: 6

Recursive function:

def factorial(n):
    if n>1: return n*factorial(n-1)
    else: return n
Output: 6

Star argument means I don’t know how many arguments. The argument is stored as a tuple.

def total(*args):
Output: 6
def proper_case(a):
    return " ".join([word[0].upper() + word[1:] for word in a.split()])
print(proper_case("going to town"))
Output: Going To Town

Lambda is a shortcut to create a function on the fly.

f = lambda a,b: a*b
Output: 6

Use map to pair a function to a list (to run the function to every element in the list)
Use filter to apply a condition to a list, to look for elements which satisfy that condition.
Use reduce to apply a function to a pair of values each time, repeatedly (reduce is in functools library)

# Using map and lambda to find words beginning with a
L= ['Apple', 'Andy', 'Banana', 'Ben']
list(map(lambda x: 1 if x[0].lower() == 'a' else 0, L))
Output: [1, 1, 0, 0]
# Using map and lambda to produce cube numbers
input_list = [1,2,3]
list(map(lambda x: x**3, input_list))
Output: [1, 8, 27]
# Using map to pair 2 lists
def v_add(x,y): return(x+y)
list1 = [1,2,3] #Argument1 is a list
list2 = (4,5,6) #Argument2 is a tuple
print(list(map(v_add, list1, list2))) #We pass 2 arguments to map
Output: [5, 7, 9]
# Using map to pair 2 lists
L1 = ['P','O']
L2 = ['X','Y']
list(map(lambda x,y: x + ' ' + y, L1, L2))
Output: ['P X', 'O Y']
# Using filter to find the even numbers 
f = lambda x:  x%2 == 0
L = [1,2,3,4,5,6]
list(filter(f, L))
Output: [2, 4, 6]

# Now using lambda
list(filter(lambda x: x % 2 == 0, L))
Output: [2, 4, 6]
# Using filter to find the words that starts with a and ends with y
L = ['Apple', 'Andy', 'Banana', 'Ben']
list(filter(lambda x: x[0].lower()=='a' and x[-1].lower()=='y', L))
Output: ['Andy']
# Using reduce to sum the input
from functools import reduce
def v_add(x,y): return(x+y) 
reduce(v_add, range(1,4))
Output: 6

# Now using lambda
reduce(lambda x, y: x+y, range(1,4))
Output: 6
# Using reduce to find the largest number
L = [22,45,32,20,87,94,30]
def v_max(x,y):
    if x>y: return x
    else: return y
#or v_max = lambda x,y: x if x>y else y
# Using reduce to concatenate letters
L = ['A','B','C']
v_concat = lambda x,y: x+y
reduce(v_concat, L)
Output: 'ABC'
# Using reduce to calculate 1 x 2 x 3 x 4 x ...
def f(x,y): return(x*y)
n = 4
L = list(range(1,n+1))
print(1 if n == 0 else reduce(f,L))
Output: 24

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

%d bloggers like this: