itertools.cycle in Python

Python Methods and Functions

Creates an iterator over the iterated values.

itertools.cycle (iterable)

-> iterator

iterable - An object that supports iterable. 


The function will create an infinite iterator , looping through the elements of an object, which is iterable . Cyclicity means that after the elements of the iterable object are exhausted, the iteration over the elements starts again.

  from itertools import cycle 


cycler = cycle ([1, 2])
next (cycler) # 1
next (cycler) # 2
next (cycler) # 1
next (cycler) # 2
next ( cycler) # 1

 This may impose additional resource requirements depending on the length of the resulting sequence.







Get Solution for free from DataCamp guru