While others gave good answers to Pythonic (I would just use the accepted answer in most cases), I just wanted to point out how easy it is to make my own utility function to do it yourself if you really prefer this:
def any_lambda(iterable, function): return any(function(i) for i in iterable) In [1]: any_lambda([1, 2, 'joe'], lambda e: isinstance(e, int) and e > 0 Out[1]: True In [2]: any_lambda([-1, '2', 'joe'], lambda e: isinstance(e, int) and e > 0) Out[2]: False
I think that at least I first defined it with a function parameter, as that would more closely match existing built-in functions such as map () and filter ():
def any_lambda(function, iterable): return any(function(i) for i in iterable)
ShawnFumo Nov 08 '13 at 20:43 2013-11-08 20:43
source share