Python itertools.zip_longest() Example


Python tip:

You can use zip_longest from itertools to zip multiple iterables of different lengths.

"Missing" values from shorter iterables are replaced by fillvalue.

An example 👇

from itertools import zip_longest

students = ["Bob", "Ann", "John", "Marry", "Daisy", "Amy"]
grades = ["A", "A+", "D"]

for student, grade in zip_longest(students, grades, fillvalue="-"):
    print(student, grade)