Python any and all: test a whole list
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.
TE-98-A: any and all on mixed and empty lists
Setup for this run, inside a throwaway directory (mktemp -d):
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]))
EOSCommand:
python3 s.pyRecorded output:
True False
False True
TrueExit 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):
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()))
EOSCommand:
python3 s.pyRecorded output:
True
FalseExit 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):
cat > s.py <<'EOS'
any(5)
EOSCommand:
python3 s.pyRecorded 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 iterableExit 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.
All full guides: All guides