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.

Discussions

Find all items from one list that are not in another list
Also there is bult-in method for sets https://docs.python.org/3/library/stdtypes.html#frozenset.difference To use it you need to convert your lists into sets diff = set(List1). difference(set(List2)) More on reddit.com
🌐 r/learnpython
13
10
December 21, 2022
python - Checking if any elements in one list are in another - Stack Overflow
There are different ways. If you just want to check if one list contains any element from the other list, you can do this.. ... I believe using isdisjoint is better than intersection for Python 2.6 and above. More on stackoverflow.com
🌐 stackoverflow.com
how to check if an element of a nested list appears in another list?
You need to "flatten" list1 into another list that doesn't contain sublists. Search for "python flatten list" to find out how to do that. Then you can use your any() approach to check that flat list against list2. You could also write a recursive function that takes two lists. It would iterate over one list, checking if the element of the list was another list or not. If not, check if the element is in the other list. If it was a list then do a recursive call checking that sublist against the other list. In fact, do both as it's a good learning experience :) More on reddit.com
🌐 r/learnpython
6
2
October 4, 2020
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-check-if-the-list-contains-elements-of-another-list
Python Check if the List Contains Elements of another List - GeeksforGeeks
July 23, 2025 - Explanation: It iterates through each element e in b and checks if it exists in a. If every element in b is found in a, it prints "Yes" otherwise prints "No". Counter() class from the collections module creates a frequency dictionary that maps each element to its count in a list. This is particularly useful when it is important to verify the number of times each element appears in the list. If we need to check whether one list contains all the elements of another list with at least the same frequency, Counter() is the ideal approach.
🌐
LabEx
labex.io › tutorials › python-how-to-check-if-all-elements-of-a-list-are-contained-in-another-list-415148
How to check if all elements of a list are contained in another list | LabEx
In this tutorial, we will explore different techniques to check if all the elements of a list are contained within another list in the context of Python programming. By the end of this guide, you will have a solid understanding of list membership and the various methods available to perform ...
🌐
Reddit
reddit.com › r/learnpython › find all items from one list that are not in another list
r/learnpython on Reddit: Find all items from one list that are not in another list
December 21, 2022 -

Hello all,

I am trying to create a list (list3) of all items that are exist in one list (list1), but do not exist in another list (list2). Any help would be greatly appreciated!

I am using pandas / data frames to create two lists each made of the items in a column in different excel files and I want to identify the items in list1 that are not found in list2.

Basically I have:

Df1 = pd.read_excel(file1)

Df2 = pd.read_excel(file2)

List1 = df1['Column Name']

List2 = df2['Column Name']

List3 = [x for x in List1 if x not in List2]

This is basically just recreating List1 and not omitting the entries that exist in List2.

Thank you all in advance! Please let me know if you need more info

🌐
TechBeamers
techbeamers.com › program-python-list-contains-elements
Python Program: Check List Contains Another List Items - TechBeamers
November 30, 2025 - While iterating the lists if we get an overlapping element, then the function returns true. The search continues until there is no element to match and returns false. # Program to check if a Python list contains elements of another list def ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-check-if-a-list-is-contained-in-another-list
Check if a List is Contained in Another List - Python - GeeksforGeeks
July 1, 2025 - A == B[i:i+n] checks if any slice matches A exactly and in order. any() returns True if at least one match is found. Using two for loops, one to go through the larger list and another to compare elements of the smaller list at each position.
🌐
Python Forum
python-forum.io › thread-36106.html
How to check if a list is in another list
January 17, 2022 - Hi All, I have the below code: list_1 = win_1 = if win_1 in list_1: print("yes")It doesn't print anything, how do I check if a list is in another list?
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python-check-if-a-list-is-contained-in-another-list
Python | Check if a list is contained in another list - GeeksforGeeks
July 23, 2025 - The task of checking if a list ... in another list. For example, checking if ["a", "b"] exists within ["a", "b", "c", "d"] would return True, while checking ["x", "y"] would return False....
🌐
TutorialsPoint
tutorialspoint.com › article › python-check-if-a-list-is-contained-in-another-list
Python - Check if a list is contained in another list
July 10, 2020 - listA = ['x', 'y', 't'] listB = ['t', 'z','a','x', 'y', 't'] print("Given listA elemnts: \n",listA) print("Given listB elemnts:\n",listB) n = len(listA) res = any(listA == listB[i:i + n] for i in range(len(listB) - n + 1)) if res: print("List A is part of list B") else: print("List A is not a part of list B") Running the above code gives us the following result − · Given listA elemnts: ['x', 'y', 't'] Given listB elemnts: ['t', 'z', 'a', 'x', 'y', 't'] List A is part of list B
🌐
Flexiple
flexiple.com › python › python-list-contains
Python list contains: How to check if an item exists in list? - Flexiple
Python offers a straightforward approach that involves iterating over each item in the list and checking for a match to find if an element exists in the list using a loop. This method is particularly useful when you need to perform additional operations on matching elements or when working with smaller lists where efficiency is less of a concern.
🌐
Finxter
blog.finxter.com › how-to-check-if-items-in-a-python-list-are-also-in-another
How to Check if Items in a Python List Are Also in Another – Be on the Right Side of Change
April 9, 2021 - To avoid writing lengthy code, Python has a range of built-in functions that meet our need to understand whether items in one list are present in another. The function any() checks if any of the items in a list are True and returns a corresponding True.
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-check-if-any-word-from-one-list-is-present-in-an-element-of-another-list-in-Python-
How to check if words from one list is present in an element of another list in Python ?
The any() function allows you to check if at least one condition in an iterable is True. When used in conjunction with list comprehension, it becomes an effective way to check whether any word from one list is present in the elements of another list..
🌐
Reddit
reddit.com › r/learnpython › is there a more efficient way to check if at least one value is present in a list of values?
r/learnpython on Reddit: Is there a more efficient way to check if at least one value is present in a list of values?
July 19, 2022 -

Let's say I have the following variables and list:

x = 1
y = 2
z = [2, 4, 6]

and I want to check if either x or y is in z. Obviously I can do this:

if x in z or y in z

but that seems very inefficient because of the repetition, especially if there are more than two values to check and if the variable/list names are much longer than single letters.

Is there a more concise way to write this that is still considered Pythonic? I know the following won't work because x (and therefore the entire if statement) will always evaluate to True, but maybe something similar:

if x or y in z

Thanks!

🌐
Codedamn
codedamn.com › news › python
Python List Contains: How to Check if an Item is in a List
June 30, 2023 - ... my_list = ["apple", "banana", "cherry"] print("banana" not in my_list) # Output: False print("pineapple" not in my_list) # Output: True · Another way to check if an item is in a list is by using the count() method.
🌐
CodeRivers
coderivers.org › blog › python-if-in-list
Python `if in list`: A Comprehensive Guide - CodeRivers
March 26, 2025 - Consider two lists, where one list contains a set of allowed values, and you want to filter another list to only include those allowed values: allowed_numbers = [1, 3, 5] original_numbers = [1, 2, 3, 4, 5] filtered_numbers = [] for number in original_numbers: if number in allowed_numbers: filtered_numbers.append(number) print(filtered_numbers)
🌐
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
Use the `any()` function to check if any element in a list meets a condition, e.g. `if any(item > 10 for item in my_list):`.