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”
Exercise instructions
- Print
my_list2
which is available in your environment. - Filter the numbers divisible by 10 from
my_list2
usingfilter()
andlambda()
. - 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:", ____)
This exercise is part of the course
Big Data Fundamentals with PySpark
Learn the fundamentals of working with big data with PySpark.
This chapter introduces the exciting world of Big Data, as well as the various concepts and different frameworks for processing Big Data. You will understand why Apache Spark is considered the best framework for BigData.
Exercise 1: What is Big Data?Exercise 2: The 3 V's of Big DataExercise 3: PySpark: Spark with PythonExercise 4: Understanding SparkContextExercise 5: Interactive Use of PySparkExercise 6: Loading data in PySpark shellExercise 7: Review of functional programming in PythonExercise 8: Use of lambda() with map()Exercise 9: Use of lambda() with filter()What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.