Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created Jul 5, 2020
Embed
What would you like to do?
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