# Python any and all: test a whole list

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-any-all-full-en
`any(items)` is true when at least one item is true. `all(items)` is true when every item is true. Only lists and one generator expression were passed. Both return a result for an empty list: `any([])` is false and `all([])` is true.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-98-A, TE-98-B and TE-98-C. The expected non-zero exit is TE-98-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-98-A: any and all on mixed and empty lists

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
print(any([0, 0, 1]), all([0, 0, 1]))
print(any([]), all([]))
print(all(x > 0 for x in [1, 2, 3]))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
True False
False True
True
```

Exit status: 0. The output matches the expected text written before the run.

### TE-98-B: any and all stop reading a generator as soon as the result is known

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
def g():
    yield True
    print('not reached')
    yield False
def h():
    yield False
    print('not reached')
    yield True
print(any(g()))
print(all(h()))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
True
False
```

Exit status: 0. This status and the output match what was written before the run.

### TE-98-C: expected non-zero exit: any of an integer raises TypeError because an integer is not iterable

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
any(5)
EOS
```

Command:

```bash
python3 s.py
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
Traceback (most recent call last):
  File "/tmp/b24-rk4t8482/work/s.py", line 1, in <module>
    any(5)
TypeError: 'int' object is not iterable
```

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-98-B: the first generator gave True and any stopped there, so not reached was not printed. The second generator gave False and all stopped there, so it printed False and nothing more. TE-98-C: any(5) stopped with exit status 1. The last line of standard error is TypeError: 'int' object is not iterable.

## Limits

Only lists and one generator expression were passed. TE-98-B shows the short-circuit behavior with two generators.

## Source

- https://docs.python.org/3/library/functions.html#any
- https://docs.python.org/3/library/functions.html#all

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-98-A` | any and all on mixed and empty lists | 0 | PASS | `5e7a0422bdce` |
| `TE-98-B` | any and all stop reading a generator as soon as the result is known | 0 | PASS | `9123ce8d8f7c` |
| `TE-98-C` | expected non-zero exit: any of an integer raises TypeError because an integer is not iterable | 1 | PASS | `702a625d051a` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python try and finally: code that always runs](https://kairos-2z6s.onrender.com/res/python-try-finally-full-en)
- [Python range: a sequence of numbers](https://kairos-2z6s.onrender.com/res/python-range-full-en)
- [Python comprehensions: build a list, dict or set in one line](https://kairos-2z6s.onrender.com/res/python-comprehensions-full-en)
- [Python argparse: read command-line arguments and get useful errors](https://kairos-2z6s.onrender.com/res/python-argparse-full-en)
- [What a 12-line pre-commit hook catches, and what it doesn't](https://kairos-2z6s.onrender.com/res/pre-commit-secrets-hook-en)
- [Python classes: a small example, line by line](https://kairos-2z6s.onrender.com/res/python-class-basics-full-en)
- [Pipes and redirection: where command output goes](https://kairos-2z6s.onrender.com/res/pipes-redirection-full-en)
