zip pairs values by position; strict=True raises ValueError if the inputs end at different positions.
Python zip(strict=True): reject a mismatched pair of input feeds
Operation contract
The receipt IDs and minor-unit amounts are bounded concrete lists. A normal zip would silently discard an unmatched trailing ID; this fixture requires the same length before a reconciliation can be accepted. The first run pairs both feeds. The second run reaches the length mismatch and rejects it. Because zip is lazy, the exception appears when the pairs are consumed, not when the zip object is constructed.
Failure and ownership boundary
A strict zip still cannot prove that two rows with the same position refer to the same real-world receipt. Validate identifiers and provenance separately. Streaming feeds can have partial side effects before a late mismatch becomes visible; stage changes before publishing them. Python iterators: exhaustion and repeatable collection ownership, Python reconciliation exercise: reject duplicate IDs before comparing ledgers and Python itertools: adjacent groups and shared iterator consumption cover those boundaries.
Working program
def pair_receipts(identifiers, amounts):
if type(identifiers) is not list or type(amounts) is not list or len(identifiers) > 10 or len(amounts) > 10:
raise ValueError("bounded feeds")
return list(zip(identifiers, amounts, strict=True))
print(pair_receipts(["R41", "R42"], [125, 75]))
try:
pair_receipts(["R41", "R42"], [125])
except ValueError:
print("length mismatch rejected")Output
[('R41', 125), ('R42', 75)]
length mismatch rejectedCosts and limits
Pairing n values takes O(n) time; materializing the result takes O(n) storage. A streaming caller can avoid that result list, but must define rollback or staging for a mismatch discovered at the end.
Common Mistakes
- Default zip truncates to the shortest input.
- Strict pairing checks length, not row identity or authorization.
Connected lessons
Python iterators: exhaustion and repeatable collection ownership, Python reconciliation exercise: reject duplicate IDs before comparing ledgers, Python itertools: adjacent groups and shared iterator consumption.
