Using any:

>>> data = [False, False, False]
>>> not any(data)
True

any will return True if there's any truth value in the iterable.

Answer from falsetru on Stack Overflow
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
How to Use all() and any() in Python | note.nkmk.me
May 12, 2025 - Therefore, all() returns True for an empty iterable. ... any() returns True if at least one element in the given iterable evaluates to true. It returns False if all elements are false.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_all.asp
Python all() Function
Python Examples Python Compiler ... Python Certificate Python Training ... The all() function returns True if all items in an iterable are true, otherwise it returns False....
๐ŸŒ
Real Python
realpython.com โ€บ python-all
Python's all(): Check Your Iterables for Truthiness โ€“ Real Python
June 16, 2023 - To check if the current item is true or false, all_true() uses the not operator to invert the truth value of its operand. In other words, it returns True if its operand evaluates to false and vice versa.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_booleans.asp
Python Booleans
When you run a condition in an if statement, Python returns True or False: Print a message based on whether the condition is True or False: a = 200 b = 33 if b > a: print("b is greater than a") else: print("b is not greater than a") Try it Yourself ยป ยท The bool() function allows you to evaluate ...
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ built-in โ€บ all
Python all() - Test If All True | Vultr Docs
September 27, 2024 - This snippet tests the list bool_list to check if every item is True. all() returns True since all values meet the condition.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ all
Python all()
Become a certified Python programmer. Try Programiz PRO! ... The all() function returns True if all elements in the given iterable are truthy. If not, it returns False.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ any-and-all-in-python
Any & All in Python?
The all() function returns True if all items in an iterable are true, otherwise it returns False.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-test-if-a-list-is-completely-true
Test if a list is Completely True - GeeksforGeeks
July 12, 2025 - all() function in Python returns True if all elements in an iterable are truthy otherwise it returns False.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2086534 โ€บ python-all-returns-true-and-any-returns-false-why
Python: all(([]) returns True and any([]) returns False, why? | Sololearn: Learn to code for FREE!
I found this definition in the python docs: any() Return True if any element of the iterable is true. If the iterable is empty, it return False. all() Return True if all elements of the iterable are true.
๐ŸŒ
Initial Commit
initialcommit.com โ€บ blog โ€บ python-all-function
Python all() Function
If the all() function uses the ... True if any value in the iterable is boolean True, and returns False only if all the values in the iterable are boolean False....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-test-for-false-list
Python | Test for False list - GeeksforGeeks
April 17, 2023 - Otherwise, recursively call the is_list_false function with the remaining elements of the list (i.e., test_list[1:]). Repeat steps 2-4 until the length of the list is 0 or the first element of the list is True. If the length of the list is 0, return True, since all the elements of the list were False.
๐ŸŒ
Data Science Parichay
datascienceparichay.com โ€บ home โ€บ blog โ€บ python โ€“ check if all elements in list are true
Python - Check if All Elements in List are True - Data Science Parichay
October 10, 2022 - Here, we applied the all() function ... thus we get True as the output. If, the above list has any falsy (evaluate to False in a boolean context) values, the all() function will result in False....
Top answer
1 of 11
442

You can roughly think of any and all as series of logical or and and operators, respectively.

any

any will return True when at least one of the elements is Truthy. Read about Truth Value Testing.

all

all will return True only when all the elements are Truthy.

Truth table

+-----------------------------------------+---------+---------+
|                                         |   any   |   all   |
+-----------------------------------------+---------+---------+
| All Truthy values                       |  True   |  True   |
+-----------------------------------------+---------+---------+
| All Falsy values                        |  False  |  False  |
+-----------------------------------------+---------+---------+
| One Truthy value (all others are Falsy) |  True   |  False  |
+-----------------------------------------+---------+---------+
| One Falsy value (all others are Truthy) |  True   |  False  |
+-----------------------------------------+---------+---------+
| Empty Iterable                          |  False  |  True   |
+-----------------------------------------+---------+---------+

Note 1: The empty iterable case is explained in the official documentation, like this

any

Return True if any element of the iterable is true. If the iterable is empty, return False

Since none of the elements are true, it returns False in this case.

all

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

Since none of the elements are false, it returns True in this case.


Note 2:

Another important thing to know about any and all is, it will short-circuit the execution, the moment they know the result. The advantage is, entire iterable need not be consumed. For example,

Copy>>> multiples_of_6 = (not (i % 6) for i in range(1, 10))
>>> any(multiples_of_6)
True
>>> list(multiples_of_6)
[False, False, False]

Here, (not (i % 6) for i in range(1, 10)) is a generator expression which returns True if the current number within 1 and 9 is a multiple of 6. any iterates the multiples_of_6 and when it meets 6, it finds a Truthy value, so it immediately returns True, and rest of the multiples_of_6 is not iterated. That is what we see when we print list(multiples_of_6), the result of 7, 8 and 9.

This excellent thing is used very cleverly in this answer.


With this basic understanding, if we look at your code, you do

Copyany(x) and not all(x)

which makes sure that, atleast one of the values is Truthy but not all of them. That is why it is returning [False, False, False]. If you really wanted to check if both the numbers are not the same,

Copyprint [x[0] != x[1] for x in zip(*d['Drd2'])]
2 of 11
58

How do Python's any and all functions work?

any and all take iterables and return True if any and all (respectively) of the elements are True.

Copy>>> any([0, 0.0, False, (), '0']), all([1, 0.0001, True, (False,)])
(True, True)            #   ^^^-- truthy non-empty string
>>> any([0, 0.0, False, (), '']), all([1, 0.0001, True, (False,), {}])
(False, False)                                                #   ^^-- falsey

If the iterables are empty, any returns False, and all returns True.

Copy>>> any([]), all([])
(False, True)

I was demonstrating all and any for students in class today. They were mostly confused about the return values for empty iterables. Explaining it this way caused a lot of lightbulbs to turn on.

Shortcutting behavior

They, any and all, both look for a condition that allows them to stop evaluating. The first examples I gave required them to evaluate the boolean for each element in the entire list.

(Note that list literal is not itself lazily evaluated - you could get that with an Iterator - but this is just for illustrative purposes.)

Here's a Python implementation of any and all:

Copydef any(iterable):
    for i in iterable:
        if i:
            return True
    return False # for an empty iterable, any returns False!

def all(iterable):
    for i in iterable:
        if not i:
            return False
    return True  # for an empty iterable, all returns True!

Of course, the real implementations are written in C and are much more performant, but you could substitute the above and get the same results for the code in this (or any other) answer.

all

all checks for elements to be False (so it can return False), then it returns True if none of them were False.

Copy>>> all([1, 2, 3, 4])                 # has to test to the end!
True
>>> all([0, 1, 2, 3, 4])              # 0 is False in a boolean context!
False  # ^--stops here!
>>> all([])
True   # gets to end, so True!

any

The way any works is that it checks for elements to be True (so it can return True), then it returnsFalseif none of them wereTrue`.

Copy>>> any([0, 0.0, '', (), [], {}])     # has to test to the end!
False
>>> any([1, 0, 0.0, '', (), [], {}])  # 1 is True in a boolean context!
True   # ^--stops here!
>>> any([])
False   # gets to end, so False!

I think if you keep in mind the short-cutting behavior, you will intuitively understand how they work without having to reference a Truth Table.

Evidence of all and any shortcutting:

First, create a noisy_iterator:

Copydef noisy_iterator(iterable):
    for i in iterable:
        print('yielding ' + repr(i))
        yield i

and now let's just iterate over the lists noisily, using our examples:

Copy>>> all(noisy_iterator([1, 2, 3, 4]))
yielding 1
yielding 2
yielding 3
yielding 4
True
>>> all(noisy_iterator([0, 1, 2, 3, 4]))
yielding 0
False

We can see all stops on the first False boolean check.

And any stops on the first True boolean check:

Copy>>> any(noisy_iterator([0, 0.0, '', (), [], {}]))
yielding 0
yielding 0.0
yielding ''
yielding ()
yielding []
yielding {}
False
>>> any(noisy_iterator([1, 0, 0.0, '', (), [], {}]))
yielding 1
True

The source

Let's look at the source to confirm the above.

Here's the source for any:

Copystatic PyObject *
builtin_any(PyObject *module, PyObject *iterable)
{
    PyObject *it, *item;
    PyObject *(*iternext)(PyObject *);
    int cmp;

    it = PyObject_GetIter(iterable);
    if (it == NULL)
        return NULL;
    iternext = *Py_TYPE(it)->tp_iternext;

    for (;;) {
        item = iternext(it);
        if (item == NULL)
            break;
        cmp = PyObject_IsTrue(item);
        Py_DECREF(item);
        if (cmp < 0) {
            Py_DECREF(it);
            return NULL;
        }
        if (cmp > 0) {
            Py_DECREF(it);
            Py_RETURN_TRUE;
        }
    }
    Py_DECREF(it);
    if (PyErr_Occurred()) {
        if (PyErr_ExceptionMatches(PyExc_StopIteration))
            PyErr_Clear();
        else
            return NULL;
    }
    Py_RETURN_FALSE;
}

And here's the source for all:

Copystatic PyObject *
builtin_all(PyObject *module, PyObject *iterable)
{
    PyObject *it, *item;
    PyObject *(*iternext)(PyObject *);
    int cmp;

    it = PyObject_GetIter(iterable);
    if (it == NULL)
        return NULL;
    iternext = *Py_TYPE(it)->tp_iternext;

    for (;;) {
        item = iternext(it);
        if (item == NULL)
            break;
        cmp = PyObject_IsTrue(item);
        Py_DECREF(item);
        if (cmp < 0) {
            Py_DECREF(it);
            return NULL;
        }
        if (cmp == 0) {
            Py_DECREF(it);
            Py_RETURN_FALSE;
        }
    }
    Py_DECREF(it);
    if (PyErr_Occurred()) {
        if (PyErr_ExceptionMatches(PyExc_StopIteration))
            PyErr_Clear();
        else
            return NULL;
    }
    Py_RETURN_TRUE;
}
Top answer
1 of 3
13
  • You can just use x[n_trues:] rather than x[n_trues:len(x)].
  • Your comments don't really say more than the code. And so I'd recommend removing the comments.
  • If you want to keep your code documented use docstrings, which can be exported to your documentation via tools like Sphinx.
  • As commented by Konrad Rudolph, you can remove the and not any(should_be_false) as this will always fail if the all fails.
def check_true_then_false(x):
    """Check first n values are True and the rest are False."""
    return all(x[:sum(x)])

If you want your code to work with iterators, not just sequences then you can instead use:

def check_true_then_false(it):
    """Check first n values are True and the rest are False."""
    it = iter(it)
    # Takes advantage of the iterating side effect, where it consumes the iterator.
    # This allows `all` to simultaneously checks `it` starts with trues and advances `it`.
    return all(it) or not any(it)

For the following two inputs all will result in:

>>> all([True] * n)
True
>>> all([True] * n + [False, ...])
False

However it will mean that it is still [...] as all and any are lazy. Meaning that we just need to check the rest are false. Meaning all slices the iterator for you without you having to. Leaving any with:

>>> any([False] * n)
False
>>> any([False] * n + [True, ...])
True
2 of 3
8

Basically, you want your list of booleans to be sorted.

Specifically, since True > False, you want your list to be sorted in decreasing order:

def check_true_then_false(booleans):
    return booleans == sorted(booleans, reverse=True)

Done!

>>> test_cases = [[True],
...               [False],
...               [True, False],
...               [True, False, False],
...               [True, True, True, False],
...               [False, True],
...               [True, False, True]]
>>> 
>>> print([check_true_then_false(test_case) for test_case in test_cases])
[True, True, True, True, True, False, False]
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ any-all-in-python
Any All in Python - GeeksforGeeks
July 23, 2025 - In this article, we will see about any and all in Python. Any Returns true if any of the items is True and returns False if empty or all are false. Any can be thought of as a sequence of OR operations on the provided iterables.
๐ŸŒ
w3reference
w3reference.com โ€บ blog โ€บ reason-for-all-and-any-result-on-empty-lists
Why Does Python's `all()` Return True and `any()` Return False for Empty Lists? The Rationale Explained โ€” w3reference.com
Similarly, "All elements in an empty list are truthy" is vacuously true. There are no elements in the list, so thereโ€™s no element that is falsy to make the statement false. Thus, all([]) returns True because the universal claim ("all elements are truthy") has no counterexamples.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-any-and-all-functions-explained-with-examples
Python any() and all() Functions โ€“ Explained with Examples
August 10, 2021 - The all() function takes an iterable as the argument, returns True only if all items in the iterable evaluate to True or if the iterable is empty. In all other cases, the all() function returns False.