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()

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

# True if any element is True
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 式Negates a boolean value.

Sample Code

# any: check if any element meets the condition
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
words = ['apple', 'banana', 'cherry']
print(any(w.startswith('b') for w in words))    # True
print(any(w.startswith('z') for w in words))    # 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': 'Alice', 'email': 'alice@example.com', 'age': 30}
user2 = {'name': 'Bob', 'email': 'bob@example.com'}
print(is_valid_user(user1))     # True
print(is_valid_user(user2))     # False (missing 'age')

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 .