Get Started

Use of lambda() with filter()

Another function that is used extensively in Python is the filter() function. The filter() function in Python takes in a function and a list as arguments. Similar to the map(), filter() can be used with lambda function. Refer to slide 6 of video 1.7 for general help of the filter() function with lambda().

In this exercise, you'll be using lambda() function inside the filter() built-in function to find all the numbers divisible by 10 in the list.

This is a part of the course

“Big Data Fundamentals with PySpark”

View Course

Exercise instructions

  • Print my_list2 which is available in your environment.
  • Filter the numbers divisible by 10 from my_list2 using filter() and lambda().
  • Print the numbers divisible by 10 from my_list2.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Print my_list2 in the console
print("Input list is:", ____)

# Filter numbers divisible by 10
filtered_list = list(____(lambda x: (x%10 == ____), my_list2))

# Print the numbers divisible by 10
print("Numbers divisible by 10 are:", ____)
Edit and Run Code