Big Data Fundamentals with PySpark
Upendra Devisetty
Science Analyst, CyVerse
Lambda functions are anonymous functions in Python
Very powerful and used in Python. Quite efficient with map()
and filter()
Lambda functions create functions to be called later similar to def
It returns the functions without any name (i.e anonymous)
Inline a function definition or to defer execution of a code
lambda arguments: expression
double = lambda x: x * 2
print(double(3))
6
def cube(x):
return x ** 3
g = lambda x: x ** 3
print(g(10))
print(cube(10))
1000
1000
No return statement for lambda
Can put lambda function anywhere
map() applies a function to all items in the input list
General syntax of map()
map(function, list)
items = [1, 2, 3, 4]
list(map(lambda x: x + 2 , items))
[3, 4, 5, 6]
filter() function takes a function and a list and returns a new list for which the function evaluates as true
General syntax of filter()
filter(function, list)
items = [1, 2, 3, 4]
list(filter(lambda x: (x%2 != 0), items))
[1, 3]
Big Data Fundamentals with PySpark