-2

I'm wondering if the all() Python function iterates over all elements of the iterable passed by param.

Dos
  • 2,250
  • 1
  • 29
  • 39

1 Answers1

3

Only if necessary. If it finds one which returns False, it bails out and returns False itself without looking at the remaining elements.

The analog any() does it likewise for the first True element. If it finds any, it aborts and returns True itself. Only if none of the elements are True, it looks at each and returns False.

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • 1
    The source code for these functions can be found here: https://github.com/python/cpython/blob/3.7/Doc/library/functions.rst They are both simple loops that return as they find a value that would determine the outcome, as you say. – Nick Vitha Mar 26 '19 at 15:50