Python – Itertools.cycle()

19 Feb, 2020
Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators. This module provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra.
There are different types of Iterators
Infinite Iterators: These type of iterators produce infinite sequences.Short sequence iterators: These iterators produces the sequences which terminate after certain iterations.Combinatorics generator functions: These generators produce the sequences in combinations related to the input arguments.
Infinite Iterators: These type of iterators produce infinite sequences.
Short sequence iterators: These iterators produces the sequences which terminate after certain iterations.
Combinatorics generator functions: These generators produce the sequences in combinations related to the input arguments.
The Function takes only one argument as input it can be like list, String, tuple, etc
The Function returns the iterator object type
In the implementation of the function the return type is yield which suspends the function execution without destroying the local variables. It is used by the generators which produce intermediate results
It iterates through each element in the input argument and yields it and repeats the cycle and produces an infinite sequence of the argument
The below-mentioned Python program illustrates the functioning of the cycle function. It takes string type as an argument and produces the infinite sequence.
import itertools    # String for sequence generationInputstring ="Geeks"  # Calling the function Cycle from# itertools and passing string as #an argument and the function returns# the iterator objectStringBuffer = itertools.cycle(Inputstring)SequenceRepeation = 0SequenceStart = 0SequenceEnd = len(Inputstring)  for output in StringBuffer:    if(SequenceStart == 0):        print("Sequence % d"%(SequenceRepeation + 1))      # Cycle function iterates through each    # element and produces the sequence     # and repeats it the sequence    print(output, end =" ")      # Checks the End of the Sequence according     # to the give input argument    if(SequenceStart == SequenceEnd-1):                  if(SequenceRepeation>= 2):            break        else:            SequenceRepeation+= 1            SequenceStart = 0            print("\n")    else:        SequenceStart+= 1
Output:
Sequence  1
G e e k s 

Sequence  2
G e e k s 

Sequence  3
G e e k s

The itertools.cycle function also works with the Python Lists. The below-mentioned Python program illustrates the functioning. It takes the Python list as an argument and produces the infinite sequence.
import itertools    # List for sequence generationInputlist = [1, 2, 3]  # Calling the function Cycle from# itertools and passing list as # an argument and the function # returns the iterator objectListBuffer = itertools.cycle(Inputlist)SequenceRepeation = 0SequenceStart = 0SequenceEnd = len(Inputlist)  for output in ListBuffer:    if(SequenceStart == 0):        print("Sequence % d"%(SequenceRepeation + 1))      # Cycle function iterates through     # each element and produces the     # sequence and repeats it the sequence    print(output, end =" ")      # Checks the End of the Sequence according    # to the give input argument    if(SequenceStart == SequenceEnd-1):                  if(SequenceRepeation>= 2):            break        else:            SequenceRepeation+= 1            SequenceStart = 0            print("\n")    else:        SequenceStart+= 1
Output:
Sequence  1
1 2 3 

Sequence  2
1 2 3 

Sequence  3
1 2 3 

Python-itertools
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Convert integer to string in Python