You can download this code by clicking the button below.
This code is now available for download.
This function combines multiple iterable objects (such as lists or tuples) into tuples, filling in missing values with fillvalue if the iterables are of uneven length.
Technology Stack : itertools
Code Type : Function
Code Difficulty : Intermediate
def zip_longest(*iterables, fillvalue=None):
# This function takes any number of iterables (like lists or tuples) and returns a list of tuples,
# using each element from the iterables as the corresponding element of the tuples.
# If the iterables are of uneven length, missing values are filled in with the fillvalue.
from itertools import zip_longest
# Example usage
list1 = [1, 2, 3]
list2 = [4, 5]
list3 = [6, 7, 8, 9]
result = zip_longest(list1, list2, list3, fillvalue=0)
return list(result)