>>> l = list(range(10))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> if filter(lambda x: x > 10, l):
... print "foo"
... else: # the list will be empty, so bar will be printed
... print "bar"
...
bar
I'd like to use any()
for this instead, but any()
only takes one argument: the iterable. Is there a better way?
Use a generator expression as that one argument:
Here the predicate is in the expression side of the generator expression, but you can use any expression there, including using functions.
Demo:
The generator expression will be iterated over until
any()
finds aTrue
result, and no further: