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

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
python - Using any() and all() to check if a list contains one set of values or another - Stack Overflow
My code is for a Tic Tac Toe game and checking for a draw state but I think this question could be more useful in a general sense. I have a list that represents the board, it looks like this: board... More on stackoverflow.com
🌐 stackoverflow.com
July 30, 2019
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 "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
🌐
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....
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,

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

any(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,

print [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.

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

>>> 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:

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

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

>>> 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:

def 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:

>>> 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:

>>> 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:

static 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:

static 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;
}
🌐
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.
Top answer
1 of 2
190

Generally speaking:

all and any are functions that take some iterable and return True, if

  • in the case of all, no values in the iterable are falsy;
  • in the case of any, at least one value is truthy.

A value x is falsy iff bool(x) == False. A value x is truthy iff bool(x) == True.

Any non-boolean elements in the iterable are perfectly acceptable — bool(x) maps, or coerces, any x according to these rules:

  • 0, 0.0, None, [], (), [], set(), and other empty collections are mapped to False
  • all other values are mapped to True.

The docstring for bool uses the terms 'true'/'false' for 'truthy'/'falsy', and True/False for the concrete boolean values.

For example:

if all(x > 0 for x in xs) or any(x > 100 for x in xs):
    # if nothing is zero or something is over a hundred …

In your specific code samples:

You’ve slightly misunderstood how these functions work. The following does something completely different from what you thought:

if any(foobars) == big_foobar:

...because any(foobars) would first be evaluated to either True or False, and then that boolean value would be compared to big_foobar, which generally always gives you False (unless big_foobar coincidentally happened to be the same boolean value).

Note: the iterable can be a list, but it can also be a generator or a generator expression (≈ lazily evaluated/generated list), or any other iterator.

What you want instead is:

if any(x == big_foobar for x in foobars):

which basically first constructs an iterable that yields a sequence of booleans—for each item in foobars, it compares the item to the value held by big_foobar, and (lazily) emits the resulting boolean into the resulting sequence of booleans:

tmp = (x == big_foobar for x in foobars)

then any walks over all items in tmp and returns True as soon as it finds the first truthy element. It's as if you did the following:

In [1]: foobars = ['big', 'small', 'medium', 'nice', 'ugly']                                        

In [2]: big_foobar = 'big'                                                                          

In [3]: any(['big' == big_foobar, 'small' == big_foobar, 'medium' == big_foobar, 'nice' == big_foobar, 'ugly' == big_foobar])        
Out[3]: True

Note: As DSM pointed out, any(x == y for x in xs) is equivalent to y in xs but the latter is more readable, quicker to write and runs faster.

Some examples:

In [1]: any(x > 5 for x in range(4))
Out[1]: False

In [2]: all(isinstance(x, int) for x in range(10))
Out[2]: True

In [3]: any(x == 'Erik' for x in ['Erik', 'John', 'Jane', 'Jim'])
Out[3]: True

In [4]: all([True, True, True, False, True])
Out[4]: False

See also: http://docs.python.org/2/library/functions.html#all

2 of 2
0

For the question in the title:

if a list contains one set of values or another

it might be more natural to use set operations. In other words, instead of

if any(x==playerOne for x in board) or any(x==playerTwo for x in board):

# or 
if playerOne in board or playerTwo in board:

use set.issubset (or set.intersection1):

if {playerOne, playerTwo}.issubset(board):

# or
if {playerOne, playerTwo} & set(board):

If playerOne and playerTwo are set/list/tuple of values, then compute their union and test if it's a subset of board:

if {*playerOne,*playerTwo}.issubset(board):

Also if the question is

if every item on the board is either playerOne marker or playerTwo marker

then instead of

if all(x == playerOne or x == playerTwo for x in board):

test set equality:1

if {playerOne, playerTwo} == set(board):

1 You can obviously assign set(board) to some variable beforehand so that you don't have to cast board to a set every time you need to test this condition.

🌐
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
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....
🌐
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.
🌐
Great Learning
mygreatlearning.com › blog › python tutorial › python booleans (true and false)
Python Booleans: True and False Values
November 24, 2025 - Notice that Zero (0), Empty text (""), and Empty brackets ([]) all resulted in False. Any variable containing real data (like 15 or "text") resulted in True. Python treats empty values as False because they represent “no data.” Non-empty ...
🌐
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.