Python
zip function
data padding
programming
software development

Is there a zip-like function that pads to longest length?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Yes. In Python, the standard-library answer is itertools.zip_longest, which behaves like zip but continues until the longest iterable is exhausted. Missing values from shorter iterables are filled with a value you choose.

Why Plain zip Is Not Enough

Built-in zip stops as soon as the shortest iterable runs out.

python
1left = [1, 2, 3, 4]
2right = ["a", "b"]
3
4print(list(zip(left, right)))

That produces only the overlapping positions and discards the remaining values from the longer iterable. That is correct when truncation is intentional, but it is wrong when you want full positional alignment.

Use itertools.zip_longest

zip_longest solves that directly.

python
1from itertools import zip_longest
2
3left = [1, 2, 3, 4]
4right = ["a", "b"]
5
6pairs = list(zip_longest(left, right, fillvalue=None))
7print(pairs)

The result preserves all positions from the longest iterable and pads the shorter one with the chosen fill value.

Choose the Fill Value Carefully

The fill value is not an implementation detail. It affects how later code interprets padded rows.

If None is already a valid data value, a unique sentinel can be safer.

python
1from itertools import zip_longest
2
3MISSING = object()
4
5for a, b in zip_longest([1, 2], [10], fillvalue=MISSING):
6    if b is MISSING:
7        print(a, "has no partner")
8    else:
9        print(a, b)

This avoids ambiguity between genuine data and artificial padding.

It Works With More Than Two Iterables

zip_longest can align any number of iterables.

python
1from itertools import zip_longest
2
3names = ["alice", "bob", "carol"]
4scores = [91, 88]
5status = ["pass", "pass", "pass", "pending"]
6
7for row in zip_longest(names, scores, status, fillvalue="MISSING"):
8    print(row)

That is useful for ragged tabular data, optional sources, and quick reporting tasks.

It Is Lazy Like zip

Like zip, zip_longest returns an iterator rather than building the full result immediately.

python
1from itertools import zip_longest
2
3
4def process_stream(a_iter, b_iter):
5    for a, b in zip_longest(a_iter, b_iter, fillvalue=0):
6        yield a + b
7
8print(list(process_stream([1, 2, 3], [10])))

That makes it suitable for streaming workflows as long as you do not immediately force everything into a list.

Infinite Iterables Need Care

Because zip_longest stops only when the longest iterable ends, it never finishes if one iterable is infinite and the others are finite.

python
1from itertools import count, zip_longest
2
3for item in zip_longest(count(), [10, 20], fillvalue=None):
4    print(item)
5    if item[0] == 4:
6        break

That behavior is correct, but it surprises people who expect the function to stop once the shorter inputs are exhausted.

When You Do Not Need Padding

Sometimes the right answer is still plain zip. If your logic should stop at the shortest iterable because partial rows are invalid, zip_longest would hide a data-quality problem instead of exposing it.

So the real question is not just whether a padded zip exists. It is whether padding is semantically correct for your data.

Common Pitfalls

The most common mistake is using plain zip and silently losing values from longer iterables.

Another common issue is choosing a fill value that can be confused with real data. Developers also often forget that zip_longest aligns by position only; it is not a keyed join and should not be used like one.

Summary

  • Use itertools.zip_longest when you need zip-like behavior padded to the longest iterable.
  • Plain zip truncates at the shortest iterable.
  • Choose the fill value deliberately so padded entries are unambiguous.
  • 'zip_longest is lazy and works well in streaming code.'
  • It solves positional alignment, not key-based joining.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.