Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. any() / all() / bool()

any() / all() / bool()

Since: Python 2(2000)

any() returns True if any element of an iterable is truthy, and all() returns True if all elements are truthy. bool() converts a value to True or False. These functions are useful when you want to apply a condition check across an entire list at once.

Syntax

any(iterable)

# True if all elements are True
all(iterable)

# Convert a value to bool
bool(value)

Function List

Function / OperatorDescription
any(iterable)Returns True if any element is truthy. Returns False if the iterable is empty.
all(iterable)Returns True if all elements are truthy. Returns True if the iterable is empty.
bool(value)Converts value to a boolean.
not expressionNegates a boolean value.

Sample Code

any_all.py
nums = [2, 4, 6, 7, 10]
print(any(n % 2 != 0 for n in nums)) # True (7 is odd)

# any: check if a specific value exists in the list
fighters = ['Iori', 'Kyo', 'Terry']
print(any(f.startswith('K') for f in fighters)) # True
print(any(f.startswith('Z') for f in fighters)) # False

# all: check if all elements meet the condition
scores = [80, 90, 75, 88]
print(all(s >= 60 for s in scores)) # True (everyone passes)

passwords = ['abc123', 'xyz', 'password!']
print(all(len(p) >= 8 for p in passwords)) # False ('xyz' is too short)

# empty iterable
print(any([])) # False (no truthy elements)
print(all([])) # True (no counterexample = vacuously true)

# bool: convert values to boolean
print(bool(0)) # False
print(bool(1)) # True
print(bool('')) # False
print(bool('hello')) # True
print(bool([])) # False
print(bool([0])) # True (a list containing 0 is truthy)
print(bool(None)) # False

# not: negate a boolean
x = 5
print(not x > 10) # True
print(not x > 0) # False

# combining any/all with conditional expressions
def is_valid_user(user):
    required = ['name', 'email', 'age']
    return all(key in user for key in required)

user1 = {'name': 'Yagami Iori', 'email': 'yagami_iori@wp-p.info', 'age': 20}
user2 = {'name': 'Kusanagi Kyo', 'email': 'kusanagi_kyo@wp-p.info'}
print(is_valid_user(user1)) # True
print(is_valid_user(user2)) # False (missing 'age')

Running the code produces the following output:

python3 any_all.py
True
True
False
True
False
False
True
False
True
False
True
False
True
False
True
False
True
False

Common Mistakes

Common Mistake 1: all([]) Returns True

For an empty iterable, all() returns True. When an empty list is passed in a context meaning "all members meet the condition," an unintended True is returned.

members = [] # empty list

# all() on an empty list always returns True
if all(score > 60 for score in members):
    print('Everyone passed') # ← prints even though there are no members

The same logic can also be written as:

if members and all(score > 60 for score in members):
    print('Everyone passed')
else:
    print('No members, or at least one failed')

Common Mistake 2: any([]) Returns False

For an empty iterable, any() returns False. When an empty list is passed to check "does anyone meet the condition," it always returns False.

candidates = []

# any() on an empty list always returns False
if not any(score > 90 for score in candidates):
    print('No high scorers') # ← prints even though there are no candidates

The same logic can also be written as:

if not candidates:
    print('No candidates')
elif not any(score > 90 for score in candidates):
    print('No high scorers')

Common Mistake 3: Using a Generator Expression Twice

A generator expression is exhausted after being consumed once. Passing the same generator to multiple functions causes the second call to receive an empty sequence.

scores = [85, 92, 78, 95, 60]
gen = (s for s in scores if s > 80)

# reusing the same generator — the second call gets an empty sequence
print(any(gen)) # True (correct)
print(all(gen)) # True (generator is empty, so all() vacuously returns True)

The same logic can also be written as:

filtered = [s for s in scores if s > 80]
print(any(filtered)) # True
print(all(filtered)) # False (78 is not included)

Practical Patterns

Common patterns for validation, permission checks, and data verification in real-world code.

from collections import namedtuple

# Character data (KOF)
Fighter = namedtuple('Fighter', ['name', 'team', 'power'])
team_japan = [
    Fighter('Kusanagi Kyo', 'Japan', 850),
    Fighter('Nikaido Benimaru', 'Japan', 780),
    Fighter('Daimon Goro', 'Japan', 820),
]

# all(): does every fighter have power >= 800?
if all(f.power >= 800 for f in team_japan):
    print('Every fighter on the Japan team is elite')
else:
    print('Japan team has a fighter below 800 power')

# any(): does anyone have power >= 850?
if any(f.power >= 850 for f in team_japan):
    print('The team has an ace-level fighter')
else:
    print('No ace-level fighter')

# any(): check for a specific element in a list
names = [f.name for f in team_japan]
print(any('Kyo' in name for name in names)) # True

# all(): validate all fields
def is_valid_fighter(f):
    return bool(f.name) and bool(f.team) and f.power > 0

print(all(is_valid_fighter(f) for f in team_japan)) # True
any_all_practice.py
python3 any_all_practice.py
Japan team has a fighter below 800 power
The team has an ace-level fighter
True
True

Notes

any() and all() use short-circuit evaluation. any() stops as soon as it finds the first truthy element, and all() stops as soon as it finds the first falsy element. When combined with a generator expression, this means subsequent elements are not evaluated once the condition is met, making it efficient.

For empty iterables, any([]) returns False and all([]) returns True. The reason all([]) is True is that there are no counterexamples to "all elements satisfy the condition" — this follows the same logic as universal quantification over an empty set in mathematics.

In Python, values such as 0, empty strings, empty lists, None, and 0.0 are treated as falsy. bool() converts values based on Python's truthiness rules (the __bool__ method).

If you find any errors or copyright issues, please .