The Wayback Machine - https://web.archive.org/web/20240717024240/https://www.geeksforgeeks.org/python-itertools-zip_longest/
Open In App

Python – Itertools.zip_longest()

Last Updated : 27 Feb, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra.

Iterators in Python is an object that can iterate like sequence data types such as list, tuple, str and so on.

Note: For more information, refer to Python Itertools

Itertools.zip_longest()

This iterator falls under the category of Terminating Iterators. It prints the values of iterables alternatively in sequence. If one of the iterables is printed fully, the remaining values are filled by the values assigned to fillvalue parameter.

Syntax:

zip_longest( iterable1, iterable2, fillval)

Example 1:




# Python code to demonstrate the working of   
# zip_longest()  
      
    
import itertools  
      
# using zip_longest() to combine two iterables.  
print ("The combined values of iterables is  : ")  
print (*(itertools.zip_longest('GesoGes', 'ekfrek', fillvalue ='_' )))  


Output:

The combined values of iterables is  : 
('G', 'e') ('e', 'k') ('s', 'f') ('o', 'r') ('G', 'e') ('e', 'k') ('s', '_')

Example 2:




from itertools import zip_longest
  
  
x =[1, 2, 3, 4, 5, 6, 7]
y =[8, 9, 10]
z = list(zip_longest(x, y))
print(z)


Output:

[(1, 8), (2, 9), (3, 10), (4, None), (5, None), (6, None), (7, None)]

Previous Article
Next Article

Similar Reads

Print first n distinct permutations of string using itertools in Python
Given a string with duplicate characters allowed, print first n permutations of given string such that no permutation is repeated. Examples: Input : string = "abcab", n = 10 Output : aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb Input : string = "okok", n = 4 Output : kkoo koko kook okko Approach: Python provides an inbuilt method to
2 min read
Python - Itertools.tee()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools tee() function This iterator splits the container into a number of it
2 min read
Python Itertools
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let's suppose there are two lists and you want to multiply their elements. There can be sever
13 min read
itertools.groupby() in Python
Prerequisites: Python Itertools Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Itertools.groupby() This method calculates the keys for each element prese
2 min read
Python - itertools.repeat()
Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools repeat() itertools.repeat()falls under the category
2 min read
Python - Itertools.takewhile()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easy. One such itertools function is takewhile(). Note: For more information, refer to Python Itertools takewhile() This allows considering an item from the iterable until t
3 min read
Python - Itertools.compress()
Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Compress() The itertools.compress() falls under the
2 min read
Python - Itertools.dropwhile()
Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Dropwhile() The d
1 min read
Python - Itertools.cycle()
Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators. This module provides various functions that work
3 min read
Python - Itertools.chain()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is a function that takes a series of iterables and r
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg