Results.

SCEPTRA.TECH

Python zip Drops the Last Rows? Use strict=True When Input Lengths Must Match

Original diagram of default zip truncation, strict length checking and output validation

Two input columns have different lengths. You pair them with Python's zip(), write the pairs to a report, and discover later that the last record vanished. By default, zip stops at the shortest input. It does not complain about the unused tail.

When the inputs are supposed to line up one-for-one, I would make that requirement explicit with strict=True. There is an important limit: the check happens during iteration, so it does not automatically make an export or database update all-or-nothing.

See the silent truncation first

names = ['Ada', 'Lin', 'Sam']
scores = [8, 9]
print(list(zip(names, scores)))

The result contains ('Ada', 8) and ('Lin', 9). Sam has no partner and does not appear. This behavior is useful when stopping at the shortest input is intentional. It is risky when a missing value represents a broken import.

Use the strict option when equal lengths are part of your data contract:

pairs = list(zip(names, scores, strict=True))

This raises ValueError for the unequal sample instead of returning a shortened list. The option was added in Python 3.10, so check the interpreter used by your actual job, not only the one in your editor.

Creating the iterator is not a completed check

The following sequence shows why the timing matters:

pairs = zip(names, scores, strict=True)
print(next(pairs))
print(next(pairs))
print(next(pairs))

The first two calls return the valid pairs. The next request discovers the mismatch and raises the error. Merely creating pairs does not consume the inputs or prove their lengths agree. Stopping a loop early can also leave the mismatch undiscovered.

I ran the sample locally: ordinary zip produced two pairs, strict zip yielded the same first two pairs, and the next request raised ValueError. A matching two-name, two-score sample completed successfully. These are small behavior checks, not tests of a production import.

Keep validation ahead of side effects when practical

For a small, finite dataset that fits comfortably in memory, building the complete list first separates this length check from later output:

pairs = list(zip(names, scores, strict=True))
# Only start the output step after this succeeds.
for name, score in pairs:
    print(name, score)

If list construction fails, the loop below it never starts. That prevents this example's output loop from running on a partial set. It does not undo side effects performed by the input iterators themselves, and it does not validate the meaning of the values.

For large streams, materializing every pair may be unsuitable. Use an output strategy designed for failure, such as a reviewed database transaction or a separate temporary export that is not treated as final until validation succeeds. Catching an exception after writing some rows does not remove those rows.

Choose a different tool when unequal lengths are valid

If the intended rule is to keep the longer input and fill gaps, itertools.zip_longest() supports that behavior with a fillvalue. Choose a fill value that cannot be confused with real data, and decide how downstream code will handle it.

Do not switch to padding just to suppress an error in inputs that should have equal lengths. Likewise, strict zip checks lengths, not identity: equal-length columns can still be in the wrong order and pair the wrong people with scores.

My final check would be the input contract: shortest-input pairing, equal-length pairing, or deliberate padding. State which one the program needs, test mismatched input, and keep that decision separate from whether the resulting records are valid.

Sources

Comments