Testing the elements of a sequence object

In Python 2.5, new built-in functions allow you to test the truth values of multiple elements of a sequence object (such as a list) or other iterables at once.

The all() function returns True if all the elements are true. It also returns True if the sequence object is empty. For example:

True

>>> myotherlist = [0, 2, 3] >>> all(myotherlist) False

>>> myemptylist = [] >>> all(myemptylist) True any()

The any() function returns True if at least one element is true (the object must not be empty). For example:

>>> any(myotherlist) True

>>> any(myemptylist) False

Was this article helpful?

0 0

Post a comment