filter function in python list code example
Example 1: filter list with python
scores = [70, 60, 80, 90, 50]
filtered = filter(lambda score: score >= 70, scores)
print(list(filtered))
# Output:
[70, 80, 90]
Example 2: filter function in python
# filter is just filters things
my_list = [1, 2, 3, 4, 5, 6, 7]
def only_odd(item):
return item % 2 == 1 # checks if it is odd or even
print(list(filter(only_odd, my_list)))