Reputation: 335
So I have been asked to use the filter()
function to iterate a function over a list so that only the positive values are returned in the list once the function is applied.
So far I have this:
def positive_place(f, xs)
ans = filter(lambda x: f(x) > 0, xs)
return ans
I have been asked specifically not to use for
loops (even though I would prefer to). I'm not too familiar with filter()
and would greatly appreciate if someone can tell me where I've gone wrong. The function above simply returns the positive values in the list I provided.
Upvotes: 2
Views: 2037
Reputation: 52181
Your filter
function is proper. But there are a few errors:
The function definition is wrong. It should include a :
in the end. Like def positive_place(f, xs):
If you are using Python 3, It return a filter object. So you will have to cast it to a list. Like return list(ans)
Your program will look like
def positive_place(f, xs):
ans = filter(lambda x: f(x) > 0, xs)
return list(ans)
Upvotes: 2