KKAIROS
Practical guideIllustrative3 min read

Python Zip: loop over lists together

zip(a, b) pairs items by position and stops at the shortest input. With strict=True unequal lengths raise ValueError.

Published:

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-77-A: zip stops at the shortest input and strict=True raises ValueError

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

cat > s.py <<'EOS'
a = [1, 2, 3]
b = ['x', 'y']
print(list(zip(a, b)))
try:
    list(zip(a, b, strict=True))
except ValueError:
    print('ValueError')
EOS

Command:

python3 s.py

Recorded output:

[(1, 'x'), (2, 'y')]
ValueError

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

TE-77-B: dict(zip(keys, values)) builds a dictionary, and zip(*pairs) turns pairs back into two tuples

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

cat > s.py <<'EOS'
print(dict(zip('ab', [1, 2])))
a, b = zip(*[(1, 'x'), (2, 'y')])
print(a, b)
EOS

Command:

python3 s.py

Recorded output:

{'a': 1, 'b': 2}
(1, 2) ('x', 'y')

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

TE-77-C: expected non-zero exit: zip with strict=True and lists of different length raises ValueError when nothing catches it

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

cat > s.py <<'EOS'
list(zip([1, 2, 3], 'ab', strict=True))
EOS

Command:

python3 s.py

Recorded output: none, standard output was empty.

Standard error from the same run:

Traceback (most recent call last):
  File "/tmp/b21-bdj9ogv9/work/s.py", line 1, in <module>
    list(zip([1, 2, 3], 'ab', strict=True))
ValueError: zip() argument 2 is shorter than argument 1

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-77-B: dict(zip('ab', [1, 2])) gave {'a': 1, 'b': 2}. zip(*pairs) gave the tuples (1, 2) and ('x', 'y'). TE-77-C: zip with strict=True and lists of 3 and 2 items was not caught. The program ended with exit status 1 and the last line of standard error is ValueError: zip() argument 2 is shorter than argument 1.

Limits

Python 3.10 added the optional strict flag to zip() (PEP 618), as the Python 3.10 What's New page in the sources says. The run used Python 3.10.12 (see the versions below). Only two inputs were tested.

Ad

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-77-A | zip stops at the shortest input and strict=True raises ValueError | 0 | PASS | f96fd3703a91 | | TE-77-B | dict(zip(keys, values)) builds a dictionary, and zip(*pairs) turns pairs back into two tuples | 0 | PASS | 0e7bae6286fc | | TE-77-C | expected non-zero exit: zip with strict=True and lists of different length raises ValueError when nothing catches it | 1 | PASS | 51417bd47778 |

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