Can this be written more concisely in a functional style

Alex Martelli aleax at
Tue Nov 18 09:35:23 CET 2003

MetalOne wrote:

> 1)
> def f(xs):
>     for x in xs:
>         if test(x): return True
>     return False
> I know that I can do (2), but it operates on the whole list and the
> original
> may break out early.  I want the efficiency of (1), but the conciseness of
> (2).
> 2)
> return True in map(test,xs)

[2] is quite different [1] in terms of semantics, of course.  [1] will
accept any true (non-false) result, such as 23 or 'foo', [2] won't.  If
[2]'s semantics are what you want,

    return True in itertools.imap(test, xs)

should give you exactly what you require.  Otherwise, you may want to
ensure a 'bool' is further applied, either by using a lambda or by
nesting two imap calls.


More information about the Python-list mailing list