Aguy Aguy - 4 months ago 19
Python Question

A neat way to create an infinite cyclic generator?

I want a generator that cycle infinitely through a list of values.

Here is my solution, but I may be missing a more obvious one.

The ingredients: a generator function that flatten an infinitely nested list, and a list appendant to itself

def ge(x):
for it in x:
if isinstance(it, list):
yield from ge(it)
else:
yield(it)


def infinitecyclegenerator(l):
x = l[:]
x.append(x)
yield from ge(x)


The use:

g = infinitecyclegenerator([1,2,3])

next(g) #1
next(g) #2
next(g) #3
next(g) #1
next(g) #2
next(g) #3
next(g) #1
...


As I said, I may be missing a trivial way to do the same and I'll be happy to learn. Is there a neater way?

Also, should I worry about memory consumption with all the mind boggling infinities going on here, or is everything cool with my code?

Answer

You can use itertools.cycle to achieve the same result

Make an iterator returning elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy.

Emphasis mine. Your only concern about memory would be saving a copy of each item returned by the iterator.

>>> from itertools import cycle
>>> c = cycle([1,2,3])
>>> next(c)
1
>>> next(c)
2
>>> next(c)
3
>>> next(c)
1
>>> next(c)
2
>>> next(c)
3
Comments