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

Answer from Erik Kaplun on Stack Overflow
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.

🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
1 month ago - Retrieve the next item from the iterator by calling its __next__() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised. ... This is the ultimate base class of all other classes. It has methods that are common to all instances of Python classes.
🌐
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....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-all-function
Python - all() function - GeeksforGeeks
July 23, 2025 - The Python all() function returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.) are True otherwise it returns False. It also returns True if the iterable object is empty.
🌐
freeCodeCamp
freecodecamp.org › news › python-any-and-all-functions-explained-with-examples
Python any() and all() Functions – Explained with Examples
August 10, 2021 - Returns True if the iterable is empty. 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.
🌐
Python Morsels
pythonmorsels.com › any-and-all
Python's any() and all() functions - Python Morsels
March 29, 2023 - This function accepts a list (or iterable) of numbers and checks whether all the numbers are between 0 and 5 (inclusive): def ratings_valid(ratings): for rating in ratings: if rating < ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use all() and any() in Python | note.nkmk.me
May 12, 2025 - Convert between bool (True/False) and other types in Python · These rules also apply when determining truth values in all() and any(). all() returns True if all elements in the given iterable evaluate to true.
Find elsewhere
🌐
Educative
educative.io › answers › how-and-when-to-use-any-and-all-in-python
How and when to use any() and all() in Python
The any() function returns True if any element in an iterable is True, otherwise it returns False. The all() function returns True if all elements in an iterable are True, otherwise it returns False.
🌐
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.
🌐
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. python Copy · 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 ...
Top answer
1 of 3
33

Your function can be reduced to this:

def checker(nums):
    return all(i <= j for i, j in zip(nums, nums[1:]))

Note the following:

  • zip loops through its arguments in parallel, i.e. nums[0] & nums[1] are retrieved, then nums[1] & nums[2] etc.
  • i <= j performs the actual comparison.
  • The generator expression denoted by parentheses () ensures that each value of the condition, i.e. True or False is extracted one at a time. This is called lazy evaluation.
  • all simply checks all the values are True. Again, this is lazy. If one of the values extracted lazily from the generator expression is False, it short-circuits and returns False.

Alternatives

To avoid the expense of building a list for the second argument of zip, you can use itertools.islice. This option is particularly useful when your input is an iterator, i.e. it cannot be sliced like a list.

from itertools import islice

def checker(nums):
    return all(i <= j for i, j in zip(nums, islice(nums, 1, None)))

Another iterator-friendly option is to use the itertools pairwise recipe, also available via 3rd party more_itertools.pairwise:

# from more_itertools import pairwise  # 3rd party library alternative
from itertools import tee

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

def checker(nums):
    return all(i <= j for i, j in pairwise(nums))

Another alternative is to use a functional approach instead of a comprehension:

from operator import le

def checker_functional(nums):
    return all(map(le, nums, nums[1:]))
2 of 3
9

Your code can in fact be reduced to checking if nums is sorted, e.g.

def checker(nums):
    return sorted(nums) == nums

This does exactly what you expect, e.g.

>>> checker([1, 1, 2, 2, 3])
True
>>> checker([1, 1, 2, 2, 1])
False
🌐
W3Schools
w3schools.com › python › python_conditions.asp
Python If Statement
All statements must be indented at the same level. ... age = 20 if age >= 18: print("You are an adult") print("You can vote") print("You have full legal rights") Try it Yourself » · Boolean variables can be used directly in if statements without comparison operators. ... Python can evaluate many types of values as True or False in an if statement.
🌐
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.
🌐
Real Python
realpython.com › python-all
Python's all(): Check Your Iterables for Truthiness – Real Python
June 16, 2023 - Luckily, Python provides the built-in all() function to solve this problem. This function takes an iterable and checks all its items for truth value, which is handy for finding out if those items have a given property or meet a particular condition.
🌐
Mostly Python
mostlypython.com › using-any
Using `any()`
January 23, 2025 - eat_dessert = False for dessert in desserts: if "apple" in dessert: eat_dessert = True break · We first set the default condition: I don't plan to eat dessert. Then we look through all the desserts, one at a time. If any of them have apple in them, I'll eat dessert.
🌐
Analytics Vidhya
analyticsvidhya.com › home › any() and all() in python with examples
any() and all() in Python with Examples - Analytics Vidhya
April 1, 2024 - # Check if all elements in the list are even numbers my_list = [2, 4, 6, 8] result = all(x % 2 == 0 for x in my_list) print(result) # Output: True · Also read: What are Functions in Python and How to Create Them?
🌐
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.