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 two articles per week.

I send two weekly emails on building performant and resilient Web Applications 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

The Built-in callable() Function

Learn about the callable() function in Python. This article explains how everything in Python is potentially callable, including classes and instances.

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

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 the more than a decade of experience I have collected 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 also a skilled writer who takes pride in breaking down technical concepts into the simplest possible terms.