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.

Christoph Schiessl

Hi, I'm Christoph Schiessl.

I help you build robust and fast Web Applications.


I'm available for hire as a freelance web developer, so you can take advantage of my more than a decade of experience working on many projects across several industries. Most of my clients are building web-based SaaS applications in a B2B context and depend on my expertise in various capacities.

More often than not, my involvement includes hands-on development work using technologies like Python, JavaScript, and PostgreSQL. Furthermore, if you already have an established team, I can support you as a technical product manager with a passion for simplifying complex processes. Lastly, I'm an avid writer and educator who takes pride in breaking technical concepts down into the simplest possible terms.

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

Telling Docker Who You Are

Learn how to avoid permission issues when creating files on a Docker bind-mount volume from within a container and manage user IDs and group IDs on Linux.

By Christoph Schiessl on DevOps and Docker

How to <link> your Blog's Atom/RSS Feed from HTML Pages

Learn how to <link> Atom and RSS feeds from your HTML documents to make them discoverable for clients and, by extension, for your readers.

By Christoph Schiessl

Web App Reverse Checklist

Ready to Build Your Next Web App?

Get my Web App Reverse Checklist first ...


Software Engineering is often driven by fashion, but swimming with the current is rarely the best choice. In addition to knowing what to do, it's equally important to know what not to do. And this is precisely what my free Web App Reverse Checklist will help you with.

Subscribe below to get your free copy of my Reverse Checklist delivered to your inbox. Afterward, you can expect one weekly email on building resilient Web Applications using Python, JavaScript, and PostgreSQL.

By the way, it goes without saying that I'm not sharing your email address with anyone, and you're free to unsubscribe at any time. No spam. No commitments. No questions asked.