In addition to all(func(i) for i in iterable)
suggested by others, you can also use itertools.imap
- all(imap(func, iterable))
. Compared to map
, imap
is an iterator thus if all
stops consuming from it, it won't advance any further.
Is there an all(map(...)) optimization in Python?
-
16-10-2022 - |
Vra
I'd like to use all(map(func, iterables))
, because it is clear, but I'm very interested in whether this approach is optimized? For example, if any calculated result of map()
is not True
mapping should stop.
Example from my project:
for item in candidate_menu:
if not item.is_max_meals_amount_ok(daily_menus):
return False
return True
I prefer to use functional-like style:
all(map(operator.methodcaller('is_max_meals_amount_ok', daily_menus), candidate_menu)
I there any optimization for all(map(...))
or any(map(...))
in Python?
Edit: Python 2.7 on board.
Oplossing
Ander wenke
If you want to get lazy evaluation in Python 2.x, try:
all(func(i) for i in iterables)
This will not build the whole list then evaluate, unlike map
. In Python 3.x, map
returns an iterator so will be lazy by default.
Edit: this answer isn't True for Python 3, where map is a generator!
No. all
and any
are short-circuited, but map
isn't: it can't know that it was called from a bit of code that does short circuiting, and always just returns the full list.
The solution is to use generator expressions:
all(func(i) for i in iterables)
That does short-circuit.
"all/any" do use short circuits.
but it wouldn't propagate across the map() command ,
to do that , change map to a generator
all(func(x) for x in iterable)
should work