Python Itertools.zip_longest() Function with Examples

Itertools Module:

Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.

Itertools in Python refers to a Python module that allows the creation of iterators, which aids in efficient looping, as well as time and space efficiency. itertools enable us to solve complex problems quickly and easily. Iterators are classified into three types.

This module provides the following types of iterators:

  1. Combinatorics Generators
  2. Infinite Iterators
  3. Terminating Iterators

itertools.zip_longest() Function :

This iterator is classified as a terminating iterator. It alternately prints the values of iterables in sequence. If one of the iterables is completely printed, the values supplied to the fillvalue argument are used to fill in the gaps.

Note:

 If no fillvalue is given it prints 'None'  to fill in the gaps.

Syntax:

zip_longest( iterable1, iterable2, fillvalue)

Parameters

iterable1, iterable2: This is Required. These are the iterables like list, string, and so on.

fillvalue: This is Optional. If the iterables are of various lengths, use this value to pad the outputs.

Examples:

Example1:

Input:

Given first List = [25, 35, 45]
Given second List = [100, 200, 300, 400, 500]

Output:

The result is : 
[(25, 100), (35, 200), (45, 300), (None, 400), (None, 500)]

Explanation:

Here it takes 25 from the first list and 100 from the second list, similarly 
it alternately prints the values of lists in sequence. But first list has 
only 3 items whereas 2 list has 5 items. Hence it fills the gaps with 'None'
as default if no fillvalue is specified.

Example2:

Input:

Given first List = "hello"
Given second List = "all"
fillvalue = '@'

Output:

The result is : 
[('h', 'a'), ('e', 'l'), ('l', 'l'), ('l', '@'), ('o', '@')]

itertools.zip_longest() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import zip_longest() function from itertools module using the import keyword.
  • Give the first list as static input and store it in a variable.
  • Give the second list as static input and store it in another variable.
  • Pass the given first and second lists as the arguments to the zip_longest() function and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import zip_longest() function from itertools module using the import keyword.
from itertools import zip_longest
# Give the first list as static input and store it in a variable.
gvn_fstlst = [25, 35, 45]
# Give the second list as static input and store it in another variable.
gvn_scndlst = [100, 200, 300, 400, 500]
# Pass the given first and second lists as the arguments to the zip_longest()
# function and store it in a variable.
rslt = zip_longest(gvn_fstlst, gvn_scndlst)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print("The result is : ")
print(rslt_lst)

Output:

The result is : 
[(25, 100), (35, 200), (45, 300), (None, 400), (None, 500)]

For Strings by giving fillvalue

Approach:

  • Import zip_longest() function from itertools module using the import keyword.
  • Give the first string as static input and store it in a variable.
  • Give the second string as static input and store it in another variable.
  • Give the fill value as static input and store it in another variable.
  • Pass the given first and second strings, and given fill value as the arguments to the zip_longest() function, and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import zip_longest() function from itertools module using the import keyword.
from itertools import zip_longest
# Give the first string as static input and store it in a variable.
gvn_fststr = "hello"
# Give the second string as static input and store it in another variable.
gvn_scndstr = 'all'
# Give the fill value as static input and store it in another variable.
gvn_filvalu = "@"
# Pass the given first and second strings and fill value as the arguments to
# the zip_longest() function and store it in a variable.
rslt = zip_longest(gvn_fststr, gvn_scndstr, fillvalue=gvn_filvalu)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print("The result is : ")
print(rslt_lst)

Output:

The result is : 
[('h', 'a'), ('e', 'l'), ('l', 'l'), ('l', '@'), ('o', '@')]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import zip_longest() function from itertools module using the import keyword.
  • Give the first list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given first and second lists as the arguments to the zip_longest() function and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import zip_longest() function from itertools module using the import keyword.
from itertools import zip_longest
# Give the first list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_fstlst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the second list as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_scndlst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
   
# Pass the given first and second lists as the arguments to the zip_longest() function
# and store it in a variable.
rslt = zip_longest(gvn_fstlst, gvn_scndlst)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print("The result is : ")
print(rslt_lst)

Output:

Enter some random List Elements separated by spaces = 9 8 7 6 5
Enter some random List Elements separated by spaces = 1 2 3
The result is : 
[(9, 1), (8, 2), (7, 3), (6, None), (5, None)]

For Strings by giving fillvalue

Approach:

  • Import zip_longest() function from itertools module using the import keyword.
  • Give the first string as user input using the input() function and store it in a variable.
  • Give the second string as user input using the input() function and store it in another variable.
  • Give the fill value as user input using the input() function and store it in another variable.
  • Pass the given first and second strings, and given fill value as the arguments to the zip_longest() function, and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import zip_longest() function from itertools module using the import keyword.
from itertools import zip_longest
# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr = input("Enter some random string = ")
# Give the second string as user input using the input() function and store it in another variable.
gvn_scndstr = input("Enter some random string = ")
# Give the fill value as user input using the input() function and store it in another variable.
gvn_filvalu = input("Enter some random character = ")
# Pass the given first and second strings and fill value as the arguments to
# the zip_longest() function and store it in a variable.
rslt = zip_longest(gvn_fststr, gvn_scndstr, fillvalue=gvn_filvalu)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print("The result is : ")
print(rslt_lst)

Output:

Enter some random string = btechgeeks
Enter some random string = hello
Enter some random character = #
The result is : 
[('b', 'h'), ('t', 'e'), ('e', 'l'), ('c', 'l'), ('h', 'o'), ('g', '#'), ('e', '#'), ('e', '#'), ('k', '#'), ('s', '#')]