-->

Infinite iterators in Python

By: Priya Philip 4 months, 2 weeks ago

An object representing a stream of data. Repeated calls to the iterator’s __next__() method (or passing it to the built-in functionnext()) return successive items in the stream. When no more data are available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its __next__() method just raise StopIteration again. Iterators are required to have an __iter__() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.

Infinite iterators:

Infinite iterators can be used to iterate indefinitely. There must be some mechanism to break out of loops that use infinite iterators.

itertools.count(start=0, step=1)

itertools.count() is similar to the built-inrange() function,which returns an infinite sequence. It is often used with map() to generate consecutive data points and with zip() to add sequence numbers. it has two arguments to control its action.

start: By setting this keyword argument we can start counting from any number we like,which defaults to 0.
step: can set an interval between numbers returned from count()

Example:


import itertools
counter = itertools.count(start=5, step=5)
list(next(counter) for _ in range(5))

Output:

[5, 10, 15, 20, 25]

count()  accepts non-integer arguments: 

Example:


count_with_floats = itertools.count(start=0.5, step=0.75)
list(next(count_with_floats) for _ in range(5))

Output:

[0.5, 1.25, 2.0, 2.75, 3.5]

 count() accepts negative numbers:

Example:

negative_count = itertools.count(start=-1, step=-0.5)
list(next(negative_count) for _ in range(5))

Output:

[-1, -1.5, -2.0, -2.5, -3.0]

itertools.cycle(iterable)

Cycle generates an infinitely repeating series of values. It takes an iterable inputs as an argument and cycle over the set of values repeatedly.

Example:

 
from itertools import *
                                  
i = 0                                                       
for item in cycle(['a', 'b', 'c']):
    i += 1
    if i == 10:
      break
    print (i, item)

Output: 

(1, 'a')
(2, 'b')
(3, 'c')
(4, 'a')
(5, 'b')
(6, 'c')
(7, 'a')
(8, 'b')
(9, 'c')


itertools.repeat(object, times):
The repeat method receives two arguments. The object argument is the value you want to repeat. The times argument is the number of times you wish to repeat that value and is optional.

Example:

from itertools import *

for i in imap(lambda x,y:(x, y, x*y), repeat(2), xrange(5)):
  print '%d * %d = %d' % i

Output: 

2 * 0 = 0
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8

Comments

Let us talk!

We take the vision which comes from dreams and apply the magic of science and mathematics, adding the heritage of our profession and our knowledge to create a design.