Butters Butters - 3 months ago 28
Python Question

filter()-parameters

Can we pass two arguments in the function that returns true and filters a list?
I am trying to get divisor from user and dividend be picked up from the list.

new_list=[1,2,3,4,5,6,7,8,9,10]
print "Get the divisor"
divisor=int(input())

h=lambda x,divisor: x%divisor==0
ans=filter(h(x,divisor),new_list)
print ans

Answer

Yes, you can, if you have divisor defined in the surrounding scope.

But then, you will not need to use it as an argument any longer:

divisor = int(raw_input())
ans = filter(lambda x: x % divisor==0, new_list)
print ans