ID EN
Itertools

zip_longest

Python 3.11 🇮🇩 Bahasa Indonesia

Buatlah iterator yang menggabungkan elemen dari masing-masing iterable.

Syntax

PYTHON
itertools.zip_longest(*iterables, fillvalue=None)

Contoh

Example 1
PYTHON
def zip_longest(*args, fillvalue=None):
     zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
    iterators = [iter(it) for it in args]
    num_active = len(iterators)
    if not num_active:
        return
    while True:
        values = []
        for i, it in enumerate(iterators):
            try:
                value = next(it)
            except StopIteration:
                num_active -= 1
                if not num_active:
                    return
                iterators[i] = repeat(fillvalue)
                value = fillvalue
            values.append(value)
        yield tuple(values)

See Also

zip_longest() islice() takewhile()