numbers = [1, 2, 3, 4]
def less_than_three(number):
return number < 3
an_iterator = filter(less_than_three, numbers)
select only numbers less than `3`
filtered_numbers = list(an_iterator)
print(filtered_numbers)
OUTPUT
[1, 2]
Alternatively, use a lambda expression for
function
.numbers = [1, 2, 3, 4]
an_iterator = filter(lambda number: number < 3, numbers)
select only numbers less than `3`
filtered_numbers = list(an_iterator)
print(filtered_numbers)
OUTPUT
[1, 2]
from : https://kite.com/python/answers/how-to-filter-a-list-in-python
No comments:
Post a Comment