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

104 views

Recent Posts

See All

Python enumerate() function

Enumerate is built in function of Python. Basically, enumerate() allows you to loop over a collection of items while keeping track of the current item’s index in a counter variable. Let’s take a look

Bangalore,Karnataka