Menu Close

How to use zip and that pads to longest length with Python?

Sometimes, we want to use zip and that pads to longest length with Python.

In this article, we’ll look at how to use zip and that pads to longest length with Python.

How to use zip and that pads to longest length with Python?

To use zip and that pads to longest length with Python, we can use the itertools.zip_longest method.

For instance, we write

l = list(itertools.zip_longest(a, b, c))

to call itertools.zip_longest with lists a, b, and c to return an iterable with tuples that has the entries of each list at the given position.

If the value doesn’t exist, it’s filled with None.

Then we convert the iterable to a list with list.

Conclusion

To use zip and that pads to longest length with Python, we can use the itertools.zip_longest method.

Posted in Python, Python Answers