The Built-in all() Function

by Christoph Schiessl on Python

One of the functions that you are sure to encounter pretty soon when you are working with boolean logic in Python is the built-in all(iterable) function. The official docs describe the function as follows:

Return True if all elements of the iterable are true (or if the iterable is empty).

This is a pretty good explanation, but the suggested implementation is even more revealing:

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

So, the function loops over the elements in the iterable, and as soon as it encounters an element that does not convert to True, it exits early by returning False. There are many other ways to implement this same function. For instance, you could use functools.reduce():

import functools

def all(iterable) -> bool:
    return bool(functools.reduce(lambda x, y: x and y, iterable, True))

Needless to say, these implementations are nothing but tools to explain how all() works. In the real world, you would never implement a function like this by yourself when an easy-to-use built-in function is available.

In practice, I often map an input_iterable to another iterable that consists of bools, which I then pass into all(). Here is a quick example for you:

Python 3.12.2 (main, Feb 17 2024, 11:13:07) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def all_double_digit(input_iterable) -> bool:
...     return all(map(lambda x: x >= 10 and x <= 99, input_iterable))
...
>>> assert all_double_digit(range(9, 100)) == False  # first element is single-digit
>>> assert all_double_digit(range(10, 100)) == True
>>> assert all_double_digit(range(10, 101)) == False # last element is triple-digit

I hope you get the idea, and please don't hesitate to reach out if you have any questions.

Ready to Learn More Web Development?

Join my Mailing List to receive 1-2 useful Articles per week.


I send up to two weekly emails on building performant and resilient Web Apps with Python, JavaScript and PostgreSQL. No spam. Unscubscribe at any time.

Continue Reading?

Here are a few more Articles for you ...


The Built-in any() Function

Learn how to use the built-in any() function in Python to determine if any element in an iterable is True, with implementation and performance insights.

By Christoph Schiessl on Python

Comparing Strings using Levenshtein Distance

Learn about the Levenshtein distance algorithm, a popular and easy-to-implement way to measure the similarity between two strings.

By Christoph Schiessl on Python

Boolean Operators and Short-Circuiting

Fully understand Python's boolean operators for negation, conjunction, and disjunction. Master operator precedence and short-circuiting.

By Christoph Schiessl on Python

Christoph Schiessl

Christoph Schiessl

Independent Consultant + Full Stack Developer


If you hire me, you can rely on more than a decade of experience, which I have collected working on web applications for many clients across multiple industries. My involvement usually focuses on hands-on development work using various technologies like Python, JavaScript, PostgreSQL, or whichever technology we determine to be the best tool for the job. Furthermore, you can also depend on me in an advisory capacity to make educated technological choices for your backend and frontend teams. Lastly, I can help you transition to or improve your agile development processes.