filtering in python code example
Example 1: python filter
number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)
Example 2: what are filters in python
In simple words, the filter() method filters the given iterable
with the help of a function that tests
each element in the iterable to be true or not.
Filter Methods is simply a like comprarator class of c++ STL
Code Explanation:
grades = ['A','B','C','F']
def remove_fails(grades):
return grades!='F'
print(list(filter(remove_fails,grades)))
Example 3: filter function in python
nums1 = [2,3,5,6,76,4,3,2]
def bada(num):
return num>4
filters = list(filter(bada, nums1))
print(filters)
(or)
bads = list(filter(lambda x: x>4, nums1))
print(bads)