Use of Lambda function in python - filter()

Big Data Fundamentals with PySpark

Upendra Devisetty

Science Analyst, CyVerse

What are anonymous functions in Python?

  • 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

Big Data Fundamentals with PySpark

Lambda function syntax

  • The general form of lambda functions is
lambda arguments: expression
  • Example of lambda function
double = lambda x: x * 2
print(double(3))
6
Big Data Fundamentals with PySpark

Difference between def vs lambda functions

  • Python code to illustrate cube of a number
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

Big Data Fundamentals with PySpark

Use of Lambda function in Python - map()

  • map() applies a function to all items in the input list

  • General syntax of map()

map(function, list)
  • Example of map()
items = [1, 2, 3, 4]
list(map(lambda x: x + 2 , items))
[3, 4, 5, 6]
Big Data Fundamentals with PySpark

Use of Lambda function in python - filter()

  • 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)
  • Example of filter()
items = [1, 2, 3, 4]
list(filter(lambda x: (x%2 != 0), items))
[1, 3]
Big Data Fundamentals with PySpark

Let's practice

Big Data Fundamentals with PySpark

Preparing Video For Download...