-
-
Save IndhumathyChelliah/c66abe74a0bae892fe7eea1d72e5620c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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