all() always returns True unless there is an element in the sequence that is False.

Your loop produces 0 items, so True is returned.

This is documented:

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

Emphasis mine.

Similarly, any() will always return False, unless an element in the sequence is True, so for empty sequences, any() returns the default:

>>> any(True for _ in '')
False
Answer from Martijn Pieters on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-all-function
Python - all() function - GeeksforGeeks
July 23, 2025 - # All elements of tuple are true t = (2, 4, 6) print(all(t)) # All elements of tuple are false t = (0, False, False) print(all(t)) # Some elements of tuple # are true while others are false t = (5, 0, 3, 1, False) print(all(t)) # Empty tuple t = () print(all(t)) # all() with condition - to check if all elements are even l = (2,4,6,8,10) print(all(ele % 2 == 0 for ele in l))
Discussions

How to check if elements in a list meet a specific condition using the 'any' and 'all' functions
additionally, all will report True for an empty list, in accordance with how mathematicians think of it (aka correctly). you can think of it as "is there not an exception?" another way of looking at it is that the and of two all statements must be the same as the all on the combined lists. e.g.: all([1,2,3]) and all([]) == all([1,2,3] + []) therefore all([]) must be True. by similar logic, any reports False to an empty list. More on reddit.com
๐ŸŒ r/pythontips
3
7
March 22, 2024
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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 21, 2026
If-else condition in assign statement in pipe chain with pandas?
You can still use np.where() inside the lambda >>> df.assign(some="value").assign(id=lambda df: np.where(df["country"] == "Switzerland", "CH", "EU")) country some id 0 Switzerland value CH 1 Poland value EU 2 Germany value EU If you modify the country column in the chain - you can see it's working on the current "piped" version of the data: >>> df.assign(country="should not match").assign(id=lambda df: np.where(df["country"] == "Switzerland", "CH", "EU")) country id 0 should not match EU 1 should not match EU 2 should not match EU Although you may want to choose a different variable name for the lambda - it can get confusing. There's also .map() which can be passed a dictionary - it's useful for multiple options: >>> df.assign(some="value").assign(id=lambda df: df["country"].map(dict(Switzerland="CH", Poland="PL")).fillna("EU")) country some id 0 Switzerland value CH 1 Poland value PL 2 Germany value EU More on reddit.com
๐ŸŒ r/learnpython
5
5
December 30, 2022
can I use an if statement with a lambda function?
It helps to debug if you split your 1 liner into multiple variable steps at each function. I haven't worked with python but it appears the problem may be in your last map. It appears what you want is to filter for len(x['pdd_list']) == 0 in a filter function. .map(lambda x: x['pdd_list']) . filter(lambda x: len(x['pdd_list'])==0) More on reddit.com
๐ŸŒ r/apachespark
4
4
August 12, 2019
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ any-and-all
Python's any() and all() functions - Python Morsels
March 29, 2023 - I very much prefer this first approach ... for loop. Python's any function and all function are for checking a condition for every item in a list (or any other iterable)....
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ built-in โ€บ all
Python all() - Test If All True | Vultr Docs
September 27, 2024 - The all() function in Python is critical for determining whether every element within an iterable (like lists, tuples, or dictionaries) meets a condition, specifically that all elements are True.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_all.asp
Python all() Function
Python Examples Python Compiler ... 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 - The loop condition relies on all() to check if all the input lists contain at least one item. In every iteration, the yield statement returns a tuple containing one item from each input list.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
How to Use all() and any() in Python | note.nkmk.me
May 12, 2025 - Passing this result to all() or any() lets you check if all or any elements meet the condition. print(all([i > 2 for i in l])) # False print(any([i > 2 for i in l])) # True ... Changing [] to () creates a generator expression, which returns a generator instead of a list. print(type([i > 2 for i in l])) # <class 'list'> print(type((i > 2 for i in l))) # <class 'generator'> ... When a generator expression is the only argument, you can omit the parentheses and pass it directly to functions like all() or any().
Find elsewhere
๐ŸŒ
Trey Hunner
treyhunner.com โ€บ 2016 โ€บ 11 โ€บ check-whether-all-items-match-a-condition-in-python
Check Whether All Items Match a Condition in Python
November 29, 2016 - ... If we want to use all in this function, we need an iterable (like a list) to pass to all. If we wanted to be really silly, we could make such a list of boolean values like this: ... If youโ€™re familiar with list comprehensions, this code ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ all
Python all()
Created with over a decade of experience and thousands of feedback. ... Try Programiz PRO! ... Become a certified Python programmer. Try Programiz PRO! ... The all() function returns True if all elements in the given iterable are truthy.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-and-when-to-use-any-and-all-in-python
How and when to use any() and all() in Python
Using a generator inside any() ... printed. Another Python function, all(), accepts an iterable and returns True if all of the elements evaluate to True; if not, it returns False....
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-any-and-all-functions-explained-with-examples
Python any() and all() Functions โ€“ Explained with Examples
August 10, 2021 - Now, this is very similar to using an if statement to check if multiple conditions chained by the logical and operator evaluate to True, as shown below: if c1 and c2 and ... c_(N-1) and CN: # DO THIS else: # DO THIS ยท You could use the all() function to make this all the more concise by collecting the conditions in an iterable, and then calling the all() function on the iterable.
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ any() and all() in python with examples
any() and all() in Python with Examples - Analytics Vidhya
April 1, 2024 - The main difference between any() and all() is their behavior when dealing with iterables. While any() returns True if at least one element satisfies the condition, all() requires all elements to meet the condition to return True.
๐ŸŒ
Codesarray
codesarray.com โ€บ view โ€บ How-to-use-all()-and-any()-functions-in-Python
How to use all() and any() functions in Python
September 20, 2024 - If every element in the iterable is truthy, all() returns True. If any element is falsy, it returns False. If the iterable is empty, all() returns True, because there are no elements that contradict the condition.
๐ŸŒ
Reddit
reddit.com โ€บ r/pythontips โ€บ how to check if elements in a list meet a specific condition using the 'any' and 'all' functions
r/pythontips on Reddit: How to check if elements in a list meet a specific condition using the 'any' and 'all' functions
March 22, 2024 -

Suppose you have a list of numbers, and you want to check if any of the numbers are greater than a certain value, or if all of the numbers are less than a certain value.

That can be done with this simple code:

# Original list
lst = [1, 2, 3, 4, 5]

# Check if any number is greater than 3
has_greater_than_3 = any(x > 3 for x in lst)

# Check if all numbers are less than 5
all_less_than_5 = all(x < 5 for x in lst)

# Print the results
print(has_greater_than_3)  # True
print(all_less_than_5)   # False

The 'any' function returns True if at least one element meets the condition, and the 'all' function returns True if all elements meet the condition.

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 โ€บ python โ€บ python_all_function.htm
Python all() Function
3 weeks ago - The Python all() function is a built-in function that returns True if all the elements of a given iterable, such as a list, tuple, set, or dictionary are truthy, and if any of the values is falsy, it returns False.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python built-in functions โ€บ python all
Python all() Function by Practical Examples - Python Tutorial
September 13, 2022 - To make it shorter, you can replace all the and operators with the all() function like this: v = 'Python' valid = all([len(v) > 0, len(v) < 25, v.isalnum()]) if valid: print(v)Code language: Python (python) In this example, The valid evaluates to True if all the conditions inside the tuple passed to the all() the function returns True.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python all() function
Python all() Function โ€“ Be on the Right Side of Change
December 4, 2020 - According to the official Python ... return False return True ยท So, it goes over all elements in the iterable and uses the element as an if condition to check whether it evaluates to True or False....
๐ŸŒ
Wiingy
wiingy.com โ€บ home โ€บ learn โ€บ python โ€บ any() and all() functions in python
Any() and All() Functions in Python (With Examples)
January 30, 2025 - Among these functions are any() and all(). Any() All() function allows us to check whether any or all elements in an iterable object meet certain conditions. In this blog post, we will explore the functionality of these two functions and how ...
๐ŸŒ
Medium
medium.com โ€บ geekculture โ€บ the-all-function-in-python-2ebb701c426
How to use the all Function in Python? | Geek Culture
September 19, 2024 - Learn how to use the Python all function. How to use the all function in Python. What is the all function? Used to check if every element evaluates to true.