Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 5, 2020 04:25
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/c66abe74a0bae892fe7eea1d72e5620c to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/c66abe74a0bae892fe7eea1d72e5620c to your computer and use it in GitHub Desktop.
import itertools
l1=[1,2,3]
#using list iterable as an argument in itertools.cycle()
l2=itertools.cycle(l1)
print (l2)#Output:<itertools.cycle object at 0x02F794E8>
count=0
for i in l2:
#It will end in infinite loop. So have to define terminating condition.
if count > 15:
break
else:
print (i,end=" ")#Output:1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1
count+=1
#string is given as an argument in itertools.cycle() function.
s1="hello"
l3=itertools.cycle(s1)
#accessing the iterator using next()
print (next(l3))#Output:h
print (next(l3))#Output:e
print (next(l3))#Output:l
print (next(l3))#Output:l
print (next(l3))#Output:o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment