python filter method 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
my_list = [1, 2, 3, 4, 5, 6, 7]
def only_odd(item):
return item % 2 == 1
print(list(filter(only_odd, my_list)))