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.
Discussions

arrays - why python all([[False, True],[False, False]]) is considered True - Stack Overflow
ndarray.all does the same thing, except it looks deeper into the array. That's the point of numpy - it broadcasts an operation to its elements. You can tweak how that's done with its parameters such as axis which would consider subsets of the array. You could do something similar with vanilla python by iterating the inner lists: >>> test = [[False... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do Python's any and all functions work? - Stack Overflow
I'm trying to understand how the any() and all() Python built-in functions work. I'm trying to compare the tuples so that if any value is different then it will return True and if they are all the same it will return False. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How "Why does all() return True if the iterable is empty?" turns on a 2,500 year old riddle in philosophy
Haven't read the article but what I think is that all() returns a false Boolean value only if there's AT LEAST ONE value in the iterable that doesn't satisfy the condition. In an empty iterable, there are no elements. So there can't be any values that don't satisfy the condition. Hence, all() returns a true value. Basically, there are only two Boolean values possible. 0(false) and 1(true). If a value can't be false, then it definitely has to be true. More on reddit.com
๐ŸŒ r/Python
36
133
March 16, 2020
How do while not loops work
In general, recall the while loop syntax. while : Conceptually, coding aside, consider filling a glass of water. Is there space for more water in the glass? Okay, then pour some. Without going in detail of how this funky glass object is defined, hopefully the following makes sense as a python analog of that process. while glass.has_space(): #we're still filling the glass glass.add_water() #glass is now full of water Without writing out how this glass object works, let's say it has another method for returning a boolean (True/False) for whether or not it is currently full, instead of the previous example of whether or not it had space for more water. We can fill the glass in the same way, but we have to negate that statement using the not operator while not glass.is_full(): #we're still filling the glass glass.add_water() #glass is now full of water Going back to the initial syntax, which is still the same, the only thing we've done is changing the condition statement of the loop. Instead of checking that glass.has_space() evaluates to True, we are now checking that the expression not glass.is_full() evaluates to True. That is the same statement as evaluating that glass.is_full() evaluates to False, because the only thing the not operator does to a boolean is negating it, i.e. True becomes False and vice versa. Now looking at your code linked, the condition for looping is that not sequenceCorrect evaluates to True, which is equivalent to the statement that sequenceCorrect is False. I won't paste the code here, but we see on line 3 that sequenceCorrect starts its life being False, so upon entering the while loop we do indeed step into that block of code because at that time not sequenceCorrect is in fact True. Then the first thing we do on line 5 is reassign it to True. If the remaining lines don't change it back to False, this will stop the while loop from repeating, since in this current state not sequenceCorrect evaluates to False. So you can say that line 5 is defaulting the loop to not going to be repeated. The only way for the loop to be repeated is for lines 6-8 with the nested for loop and if statement to find some character in dna that is not in "actgn", upon which sequenceCorrect will be assigned to False and thus the statement in the while loop not sequenceCorrect would evaluate to True and thus the loop would repeat itself once more. Personally, I think this is just a pretty unclear way to achieve the goal of checking the validity of that input string. I would have defined it another way, but I can't see anything wrong with it really. If the not operation in the while loop statement is bothering you, you could equally have rewritten the code with a variable for instance named sequenceIncorrect and just flipped all assignments i.e. True's become False's and vice versa. More on reddit.com
๐ŸŒ r/learnpython
16
5
September 16, 2024
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ any-and-all-in-python
Any & All in Python?
July 30, 2019 - The all() function returns True if all items in an iterable are true, otherwise it returns 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.
๐ŸŒ
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!
December 2, 2019 - 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.
๐ŸŒ
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
May 12, 2025 - 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;
}
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ truthy-and-falsy-values-in-python
Truthy and Falsy Values in Python: A Detailed Introduction
January 22, 2020 - According to the Python Documentation: By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object.