Infinite Iterators in Python Last Updated : 06 Dec, 2019 Comments Improve Suggest changes Like Article Like Report Iterator in Python is any python type that can be used with a ‘for in loop’. Python lists, tuples, dictionaries, and sets are all examples of inbuilt iterators. But it is not necessary that an iterator object has to exhaust, sometimes it can be infinite. Such type of iterators are known as Infinite iterators. Python provides three types of infinite iterators - count(start, step): This iterator starts printing from the “start” number and prints infinitely. If steps are mentioned, the numbers are skipped else step is 1 by default. See the below example for its use with for in loop. Example: Python3 1== # Python program to demonstrate # infinite iterators import itertools # for in loop for i in itertools.count(5, 5): if i == 35: break else: print(i, end =" ") Output: 5 10 15 20 25 30 cycle(iterable): This iterator prints all values in order from the passed container. It restarts printing from beginning again when all elements are printed in a cyclic manner. Example #1: Python3 1== # Python program to demonstrate # infinite iterators import itertools count = 0 # for in loop for i in itertools.cycle('AB'): if count > 7: break else: print(i, end = " ") count += 1 Output: A B A B A B A B Example #2: Using next function. Python3 1== # Python program to demonstrate # infinite iterators import itertools l = ['Geeks', 'for', 'Geeks'] # defining iterator iterators = itertools.cycle(l) # for in loop for i in range(6): # Using next function print(next(iterators), end = " ") Output: Geeks for Geeks Geeks for Geeks repeat(val, num): This iterator repeatedly prints the passed value infinite number of times. If the optional keyword num is mentioned, then it repeatedly prints num number of times. Example: Python3 1== # Python code to demonstrate the working of # repeat() # importing "itertools" for iterator operations import itertools # using repeat() to repeatedly print number print ("Printing the numbers repeatedly : ") print (list(itertools.repeat(25, 4))) Output: Printing the numbers repeatedly : [25, 25, 25, 25] Comment More infoAdvertise with us Next Article Infinite Iterators in Python N nikhilaggarwal3 Follow Improve Article Tags : Python Python-itertools Practice Tags : python Similar Reads Iterators in Python An iterator in Python is an object that holds a sequence of values and provide sequential traversal through a collection of items such as lists, tuples and dictionaries. . The Python iterators object is initialized using the iter() method. It uses the next() method for iteration.__iter__(): __iter__ 3 min read Combinatoric Iterators in Python An iterator is an object that can be traversed through all its values. Simply put, iterators are data type that can be looped upon. Generators are iterators but as they cannot return values instead they yield results when they are executed, using the 'yield' function. Generators can be recursive jus 4 min read Python | Decimal is_infinite() method Decimal#is_infinite() : is_infinite() is a Decimal class method which checks whether the Decimal value is infinite value. Syntax: Decimal.is_infinite() Parameter: Decimal values Return: true - if the Decimal value is infinite value; otherwise false Code #1 : Example for is_infinite() method Python3 2 min read Python infinity (inf) The concept of representing infinity as an integer violates the definition of infinity itself. As of 2020, there is no such way to represent infinity as an integer in any programming language so far.But in Python, as it is a dynamic language, float values can be used to represent an infinite integer 3 min read Python - Itertools.islice() 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 islice(). Note: For more information, refer to Python Itertools islice() function This i 2 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 numpy.isfinite() in Python The numpy.isfinite() function tests element-wise whether it is finite or not(not infinity or not Not a Number) and return the result as a boolean array. Syntax :Â numpy.isfinite(array [, out]) Parameters :Â array : [array_like]Input array or object whose elements, we need to test for infinity out : 2 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 | Decimal is_finite() method Decimal#is_finite() : is_finite() is a Decimal class method which checks whether the Decimal value is finite value. Syntax: Decimal.is_finite() Parameter: Decimal values Return: true - if the Decimal value is finite value; otherwise false Code #1 : Example for is_finite() method Python3 # Python Pro 2 min read Operator.length_hint() method in Python length_hint() method is part of the operator module, which contains a collection of functions that correspond to standard operations and can be useful for functional programming and optimization.How operator.length_hint() WorksThe length_hint() function is designed to interact with objects that impl 2 min read Like