# Python Zip: loop over lists together

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-zip-full-en
`zip(a, b)` pairs items by position and stops at the shortest input. With `strict=True` unequal lengths raise `ValueError`.

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-77-A, TE-77-B and TE-77-C. The expected non-zero exit is TE-77-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes 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`):

```bash
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:

```bash
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`):

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

Command:

```bash
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`):

```bash
cat > s.py <<'EOS'
list(zip([1, 2, 3], 'ab', strict=True))
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/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.

## Source

- https://docs.python.org/3/library/functions.html#zip
- https://docs.python.org/3/whatsnew/3.10.html

## 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.

## Related guides
- [Python List Slicing: take part of a list](https://kairos-2z6s.onrender.com/res/python-list-slicing-full-en)
- [Python Loop Else: runs when the loop did not break](https://kairos-2z6s.onrender.com/res/python-loop-else-full-en)
- [Python range: a sequence of numbers](https://kairos-2z6s.onrender.com/res/python-range-full-en)
- [Removing a committed secret from all Git history](https://kairos-2z6s.onrender.com/res/remove-secret-from-git-history-en)
- [Python With Statement: files closed for you](https://kairos-2z6s.onrender.com/res/python-with-statement-full-en)
- [Restore a deleted branch with git reflog](https://kairos-2z6s.onrender.com/res/restore-deleted-branch-full-en)
- [python -m venv: a separate package folder for one project](https://kairos-2z6s.onrender.com/res/python-venv-full-en)
