Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Big Data Fundamentals with PySpark

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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:", ____)
Code bewerken en uitvoeren