any():

if any(t < 0 for t in x):
    # do something

Also, if you're going to use "True in ...", make it a generator expression so it doesn't take O(n) memory:

if True in (t < 0 for t in x):
Answer from Ken on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_any.asp
Python any() Function
Python Examples Python Compiler ... Python Certificate Python Training ... The any() function returns True if any item in an iterable are true, otherwise it returns False....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-any-function
Python any() function - GeeksforGeeks
July 23, 2025 - In this example, the any() function is used to check if any value in the list is True. If at least one element in the Python list is True, it will return 'True'; otherwise, it will return 'False'.
Discussions

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
What in-house tools are you building or using for network automation?
A lot of teams Iโ€™ve seen build a mix of Python scripts and Ansible playbooks on top of NetBox or Nautobot for inventory, config generation, and drift detection. The biggest wins are usually automating repetitive tasks and having a single source of truth, but integration and keeping everything up to date can get tricky fast. More on reddit.com
๐ŸŒ r/networking
75
58
1 month ago
Is there a way of saying "If any condition resulted in True do A else B"
๐ŸŒ r/Python
82
10
May 19, 2023
Is there a more efficient way to check if at least one value is present in a list of values?
You can use any with a generator. if any(item in z for item in x, y): This will short-circuit, ie it will exit as soon as it finds an item that fulfils the criteria. More on reddit.com
๐ŸŒ r/learnpython
7
1
July 19, 2022
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-any-function
Python any() Function: Guide With Examples and Use Cases | DataCamp
July 31, 2024 - The any() function in Python returns True if at least one element in an iterable (list, tuple, set, etc.) is true, and False otherwise.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ any
Python any()
Become a certified Python programmer. Try Programiz PRO! ... The any() function returns True if any element of an iterable is True. If not, it returns False. ... The any() function takes an iterable (list, string, dictionary etc.) in Python.
๐ŸŒ
Real Python
realpython.com โ€บ any-python
How to Use any() in Python โ€“ Real Python
February 18, 2022 - Pythonโ€™s any() and or return different types of values. any() returns a Boolean, which indicates whether it found a truthy value in the iterable: ... In this example, any() found a truthy value (the integer 1), so it returned the Boolean value ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_conditions.asp
Python If Statement
The if statement evaluates a condition (an expression that results in True or False). If the condition is true, the code block inside the if statement is executed. If the condition is false, the code block is skipped.
Find elsewhere
๐ŸŒ
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( ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ functions.html
Built-in Functions โ€” Python 3.14.3 documentation
3 weeks ago - Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to: def any(iterable): for element in iterable: if element: return True return False
๐ŸŒ
Mostly Python
mostlypython.com โ€บ using-any
Using `any()`
January 23, 2025 - It feels like I should be able to use the one-liner approach without always going through a round of refactoring. It turns out using any() in the real world is a bit more complicated than it appears on the surface. Python makes it quite straightforward to determine if a specific item appears in a collection.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-any-and-all-functions-explained-with-examples
Python any() and all() Functions โ€“ Explained with Examples
August 10, 2021 - Let's understand the syntax of the any() function, look at some simple examples, and then proceed to more useful examples. ... Returns True if bool(x) is True for any x in the iterable.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ any-and-all
Python's any() and all() functions - Python Morsels
March 29, 2023 - Let's talk about using Python's any and all functions to check whether all iterable items match a condition. This function accepts a list (or iterable) of numbers and checks whether all the numbers are between 0 and 5 (inclusive): def ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ any-and-all-in-python-with-examples
any() and all() in Python with Examples
September 11, 2023 - Similarly to or, we can chain multiple and operators, and they will evaluate to True only if all the operands evaluate to True: print(str(True and True and True and False and True)) ... The method any(iterable) behaves like a series of or operators between each element of the iterable we passed.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-and-when-to-use-any-and-all-in-python
How and when to use any() and all() in Python
On the other hand, the second print statement prints False because none of the elements are True, i.e., all elements are False. The any() function checks if at least one character in the string is a digit.
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-check-if-any-element-in-list-satisfies-a-condition
Check if any element in list satisfies a condition-Python - GeeksforGeeks
July 12, 2025 - For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the condition. any() is the most efficient way to check if any element in a list satisfies a condition.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ built-in โ€บ any
Python any() - Check Any True Values | Vultr Docs
September 27, 2024 - The any() function in Python checks if any element in an iterable is True. This function is extremely useful when you need a quick way to evaluate if at least one of the conditions in a composite query returns True.
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.

๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ any() and all() in python with examples
any() and all() in Python with Examples - Analytics Vidhya
April 1, 2024 - Checking for Specific Attributes in a List of Dictionaries ... The any() follows a straightforward syntax: any(iterable) yields True if at least one element within the iterable is evaluated as true and False otherwise.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ reference โ€บ compound_stmts.html
8. Compound statements โ€” Python 3.14.3 documentation
Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. In general, compound statements span multiple lines, although in simple incarnations a whole compound statement may be contained in one line. The if, while and for statements implement traditional control flow constructs.