Search
  • livewithcs

Python filter() function

1. The filter() alternative a for loop but it is a builtin function and faster.

2. The filter() creates a list of elements for which a function returns true .



Let’s take a look at an example:


number_list = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,9 , 10]
nums_less_than_eq_five = list(filter(lambda x: x <= 5, number_list))
print(nums_less_than_eq_five)
# Output: [1, 2, 3, 4, 5]


Please find the youtube video on this topic URL: https://www.youtube.com/watch?v=qJhRKwxACI4

0 views

Bangalore,Karnataka