Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 6, 2020 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/bd8d4b11ceb1fb27056eaa268826cb8b to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/bd8d4b11ceb1fb27056eaa268826cb8b to your computer and use it in GitHub Desktop.
import itertools
#fillvalue is not given,by default it will be None.
#iteration continues until longest iterable is exhausted.
z1=itertools.zip_longest([1,2,3,4,5],['a','b','c'])
print (z1)#Output:<itertools.zip_longest object at 0x00AA3BE0>
#we can iterate through zip object using for loop or we can convert to list object.
print (list(z1))#Output:[(1, 'a'), (2, 'b'), (3, 'c'), (4, None), (5, None)]
#fillvalue is mentioned
z2=itertools.zip_longest([1,2,3,4,5],['a','b','c'],fillvalue="z")
print (list(z2))#Output:[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'z'), (5, 'z')]
#using zip(),iteration continues until shorest iterable is exhausted.
z3=zip([1,2,3,4,5],['a','b','c'])
print (list(z3))#Output:[(1, 'a'), (2, 'b'), (3, 'c')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment