4 ms·
If you have enough elements to desire short circuit evaluation you can, as `masklinn` mentioned, use a list comprehension. Specifically a generator list compre
by knome 17y ago
If you have enough elements to desire short circuit evaluation you can, as `masklinn` mentioned, use a list comprehension. Specifically a generator list comprehension.
http://www.python.org/dev/peps/pep-0289/ http://www.python.org/dev/peps/pep-0289/
big_list = make_big_list()
if any( not prime( a ) for a in big_list ) :
do_thing()
When you surround the list comprehension syntax with parenthesis instead of square brackets, either as a lone argument to a function or independently, you create a generator that will iterate once over the given data.
This allows for functions accepting sequences to short circuit without completing the function mapping.