Filtering Lists with filter() in Python


Use filter() to create a list of elements for which a function returns True.

Source Code

def is_even(number):
    return number % 2 == 0
even_numbers = filter(is_even, [1, 2, 3, 4, 5])
print(list(even_numbers))
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments