Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pierian-data

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: pierian-data/complete-python-3-bootcamp
Path: blob/master/09-Empty-Section-Skip/06-all() and any().ipynb
Views: 648
Kernel: Python 3


Content Copyright by Pierian Data

all() and any()

all() and any() are built-in functions in Python that allow us to conveniently check for boolean matching in an iterable. all() will return True if all elements in an iterable are True. It is the same as this function code:

def all(iterable): for element in iterable: if not element: return False return True

any() will return True if any of the elements in the iterable are True. It is equivalent to the following function code:

def any(iterable): for element in iterable: if element: return True return False

Let's see a few examples of these functions. They should be fairly straightforward:

lst = [True,True,False,True]
all(lst)
False

Returns False because not all elements are True.

any(lst)
True

Returns True because at least one of the elements in the list is True

There you have it, you should have an understanding of how to use any() and all() in your code.