IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Big Data Fundamentals with PySpark

Visualizza il corso

Istruzioni dell'esercizio

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

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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:", ____)
Modifica ed esegui il codice