converting boolean filter function to lambda

Ethan Furman ethan at stoneleaf.us
Thu Jun 25 11:59:41 EDT 2015


I have the following function:

def phone_found(p):
   for c in contacts:
     if p in c:
       return True
   return False

with the following test data:

contacts = ['672.891.7280 x999', '291.792.9000 x111']
main = ['291.792.9001', '291.792.9000']

which works:

filter(phone_found, main)
# ['291.792.9000']

My attempt at a lambda function fails:

filter(lambda p: (p in c for c in contacts), main)
# ['291.792.9001', '291.792.9000']

Besides using a lambda ;) , what have I done wrong?

--
~Ethan~



More information about the Python-list mailing list