When I try to filter [1,2,0,3,8] with if x < 3: return x I end up with [1,2]. Why is the 0 not included in this list?
def TestFilter(x):
if x < 3:
return x
a = [1,2,0,3,8]
b = filter(TestFilter, a)
print b
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Every time your function returns
Truefilter() will add the current element from the original list to the new list. Python considers0to beFalseand any other number to beTrue. Therefore you will want to have the function returnTrueinstead of the number.EDIT: Here’s a lambda example: