The best answer here is to use all(), which is the builtin for this situation. We combine this with a generator expression to produce the result you want cleanly and efficiently. For example:

>>> items = [[1, 2, 0], [1, 2, 0], [1, 2, 0]]
>>> all(flag == 0 for (_, _, flag) in items)
True
>>> items = [[1, 2, 0], [1, 2, 1], [1, 2, 0]]
>>> all(flag == 0 for (_, _, flag) in items)
False

Note that all(flag == 0 for (_, _, flag) in items) is directly equivalent to all(item[2] == 0 for item in items), it's just a little nicer to read in this case.

And, for the filter example, a list comprehension (of course, you could use a generator expression where appropriate):

>>> [x for x in items if x[2] == 0]
[[1, 2, 0], [1, 2, 0]]

If you want to check at least one element is 0, the better option is to use any() which is more readable:

>>> any(flag == 0 for (_, _, flag) in items)
True
Answer from Gareth Latty on Stack Overflow
🌐
Vultr Docs
docs.vultr.com › python › built-in › all
Python all() - Test If All True | Vultr Docs
September 27, 2024 - Apply the all() function to determine if all conditions are true. ... x = 5 y = 10 conditions = [x < 10, y > 5, x + y == 15] result = all(conditions) print(result) Explain Code · In this example, all conditions in the list conditions are True ...
🌐
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. It efficiently checks entire list without requiring explicit iteration.
🌐
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 - You can use the Python built-in all() function to check if all the elements in a list are True or not. Pass the list as an argument to the function.
🌐
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.

🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use all() and any() in Python | note.nkmk.me
May 12, 2025 - In Python, you can use the built-in functions all() and any() to check whether all elements or at least one element in an iterable (such as a list or tuple) evaluate to True. Built-in Functions - all( ...
🌐
Reintech
reintech.io › blog › python-using-all-any-methods
Python: Using the all() and any() Methods | Reintech media
January 4, 2026 - In Python, the all() method is a built-in function that checks if all elements in an iterable (like list, tuple, set or dictionary) are true. If they are, the function returns 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.
Find elsewhere
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]
🌐
W3Schools
w3schools.com › python › ref_func_all.asp
Python all() Function
The all() function returns True if all items in an iterable are true, otherwise it returns False. If the iterable object is empty, the all() function also returns True. ... Note: When used on a dictionary, the all() function checks if all the ...
🌐
YouTube
youtube.com › watch
How To Check If All List Elements Are True In Python - YouTube
In this python tutorial, I show you how to check if all list elements are true in python! We talk about truthy and falsy values in python and how that can ef...
Published   July 15, 2024
🌐
McNeel Forum
discourse.mcneel.com › grasshopper
Test if all values of a list is True or False using Python - Grasshopper - McNeel Forum
April 22, 2018 - Hi everyone. I’m trying to create a python script that reads a list of values ​​and tests if they satisfy the condition, like the image below. [help1] However, I’d like it to return only one True if all values ​​satis…
🌐
Career Karma
careerkarma.com › blog › python › how to use python any and all: a step-by-step guide
How to Use Python Any and All: A Step-By-Step Guide | Career Karma
December 1, 2023 - That’s where the Python built-in functions any() and all() come in. any() iterates through every item in an object and returns True if any item is equal to True. all() goes through every item in an object and returns True only if every item in the object is equal to True. This tutorial will discuss how to use the any() and all() methods in Python, and explore an example of each of these methods in a program. The Python any() method ...
🌐
Bobby Hadz
bobbyhadz.com › blog › python-check-if-any-element-in-list-meets-condition
Check if all/any elements in List meet condition in Python | bobbyhadz
Copied!my_list = [1, 3, 5, 15] ... > 10 for item in my_list)) ... The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty...
🌐
GeeksforGeeks
geeksforgeeks.org › python-check-if-all-elements-in-list-follow-a-condition
Python | Check if all elements in list follow a condition - GeeksforGeeks
April 12, 2023 - Explanation: Using map() function, we can apply the given condition to each element in the list and return a list of True or False. The any() function will check if any of the element in the returned list is False, which means that not all elements ...
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.

🌐
Programguru
programguru.org › home › python course › python all() function – check if all elements are true
Python all() Function – Check If All Elements Are True
Learn how Python's all() function works. Check if all elements in a list, tuple, or iterable are true. Includes syntax, examples, use cases, and common mistakes.
🌐
Trey Hunner
treyhunner.com › 2016 › 11 › check-whether-all-items-match-a-condition-in-python
Check Whether All Items Match a Condition in Python
For our purposes, we’ll treat it as pretty much the same as True. ... Notice the similarity between all and our is_prime function? Our is_prime function is similar, but they’re not quite the same structure.