Sitelet https://howtodoinjava.com/python-builtin/python-all/

Python all() Function: Check If All Elements Are True

Python all() returns True when every element is truthy, and for an empty iterable. See dict and string examples, why empty is True, and validation.

Python

The Python all() function returns True when every element of an iterable is truthy, and also when the iterable is empty; it returns False as soon as one element is falsy. It works like a logical AND over all elements, and validation code uses it to check that every field, row or item passes a rule.

mytuple = (0, 1, False)
result = all(mytuple)          # False, because 0 is falsy

This article covers the syntax, all() on tuples, lists, dictionaries and strings, the empty-iterable case that surprises many developers, and validation patterns. Every result comes from Python 3.14.

1. Syntax and Return Value

The function takes one iterable and returns a bool. It is False only when at least one element is falsy.

all(iterable) -> bool
Elements of the iterableall() returns
All truthyTrue
Some truthy, some falsyFalse
All falsyFalse
No elements (empty)True

2. all() with Tuple, List, Dictionary and String

A tuple or list passes when none of its values is 0, False, None or empty. A dictionary is checked by its keys, and a string by its characters.

t1 = all((1, 2, True))            # True
t2 = all((0, 1, False))           # False
l = all([1, 2, 3])                # True
d1 = all({0: "False"})            # False, the key 0 is falsy
d2 = all({1: "True", 2: "True"})  # True
s1 = all("abc")                   # True
s2 = all("")                      # True, an empty string has no characters

The last line corrects a common mistake. An empty string makes all() return True, not False, because there is no character that could fail.

3. Why all() Returns True for an Empty Iterable

The function looks for a falsy element and returns True when it finds none, and an empty iterable has none. The equivalent loop from the Python docs makes that visible.

def all_like(iterable):
    for element in iterable:
        if not element:
            return False
    return True

empty_result = all_like([])       # True

That rule causes real bugs. A check such as all(price > 0 for price in prices) passes when prices is empty, so we test for an empty list separately when an empty input is invalid.

prices = []
valid = bool(prices) and all(p > 0 for p in prices)   # False

4. Validating Data with all()

A generator expression lets all() apply a rule to every item, and it stops at the first failure. For required dictionary keys, a set comparison on keys() reads even shorter.

order = {"id": 7, "qty": 2, "price": 9.5}
required = {"id", "qty", "price"}
has_keys = all(k in order for k in required)   # True
has_keys2 = required <= order.keys()           # True

qtys = [2, 1, 0, 4]
all_positive = all(q > 0 for q in qtys)        # False, stops at 0

To test whether at least one element is truthy, use Python any(). Note that all(x) equals not any(not e for e in x), not not any(x).

5. Validating Every Row of Imported Data

Before importing rows from a CSV file, we check that every value in a column has the right form. One all() call per rule keeps the checks readable, and each stops at the first bad row.

rows = [["tea", "4"], ["milk", "2"], ["rice", "x"]]
names_ok = all(name.isalpha() for name, qty in rows)     # True
qty_ok = all(qty.isdigit() for name, qty in rows)        # False
bad = [row for row in rows if not row[1].isdigit()]      # [['rice', 'x']]

The all() call answers yes or no, so a second pass with a comprehension finds the rows to report. For large files, we run that second pass only when the check fails.

6. all() on Equal Values, Types and Empty Input

These questions show where all() helps and where another tool reads better.

6.1. How do we check whether all elements of a list are equal?

We compare each element with the first one, or count the distinct values with a set when the elements are hashable.

values = [3, 3, 3]
same1 = all(v == values[0] for v in values)   # True
same2 = len(set(values)) <= 1                 # True

6.2. How do we check that all elements have one type?

We combine all() with isinstance(), all(isinstance(x, int) for x in items). Note that True passes this check, because bool is a subclass of int.

6.3. Does all() work on a dictionary’s values?

Only through values(). Passing the dictionary checks its keys, so all({“a”: 0}) is True while all({“a”: 0}.values()) is False.

7. Conclusion

The all() function returns True only when no element is falsy, which includes the empty case. We pair it with a generator expression for validation, and we add an explicit emptiness check when an empty input should fail.

8. References

Happy Learning !!

Source Code on Github

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.