I am trying to understand how the filter() function works and would like to know
how I could write:
test = filter(lambda x: x == y, lst)
using for or while loops.
I am trying to understand how the filter() function works and would like to know
how I could write:
test = filter(lambda x: x == y, lst)
using for or while loops.
filter() is pretty much making a new list with a for loop and a conditional. In your example, it is identical to:
L = []
for i in lst:
if i == y:
L.append(i)
Or as a list comprehension:
[i for i in lst if i == y]