Python - Itertools.cycle() Last Updated : 03 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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.Itertools.cycle()The Function takes only one argument as input it can be like list, String, tuple, etcThe Function returns the iterator object typeIn 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 resultsIt 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. Python3 import itertools # String for sequence generation Inputstring ="Geeks" # Calling the function Cycle from # itertools and passing string as #an argument and the function returns # the iterator object StringBuffer = itertools.cycle(Inputstring) SequenceRepeation = 0 SequenceStart = 0 SequenceEnd = 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 given 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. Python3 import itertools # List for sequence generation Inputlist = [1, 2, 3] # Calling the function Cycle from # itertools and passing list as # an argument and the function # returns the iterator object ListBuffer = itertools.cycle(Inputlist) SequenceRepeation = 0 SequenceStart = 0 SequenceEnd = 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 given 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 Comment More infoAdvertise with us Next Article Python - itertools.repeat() Y Yelleti_Varshith Follow Improve Article Tags : Python Python-itertools Practice Tags : python Similar Reads Python Itertools Python's Itertool is a module that 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. For example, let's suppose there are two lists and we wa 12 min read Python - Itertools.count() Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it co 3 min read Python - Itertools.cycle() 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 3 min read Python - itertools.repeat() Pythonâs Itertool is a module that 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. Note: For more information, refer to Python Itertools re 2 min read Itertools.accumulate()-Python itertools.accumulate() is an iterator that takes two arguments, an iterable (target) and an optional function. The function is applied at each iteration to accumulate the result. By default, if no function is provided, it performs addition. If the input iterable is empty, the output will also be emp 3 min read Python - Itertools.chain() The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is 4 min read Python - Itertools.chain.from_iterable() Python's Itertool is a module that 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. Note: For more information, refer to Python Itertools Th 2 min read Python - Itertools.compress() Pythonâs Itertool is a module that 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. Note: For more information, refer to Python Itertools Co 2 min read Python - Itertools.dropwhile() Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more inform 1 min read Python - Itertools.filterfalse() In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools filterfalse() funct 2 min read Like