How to yield element multiple times

generator, python

Solution

Loop again:

def rgen (n):
    for elem in list:
        for times in xrange(n):
            yield elem

I'd probably write a helper function similar to:

from itertools import chain, repeat

def repeated(iterable, n=1):
    items = chain.from_iterable(repeat(item, n) for item in iterable)
    for item in items:
        yield item

    # Or, in Python3.3 you could do:
    yield from items

Problem

I am trying to implement a method, in which each element of list gets yielded as much as the paramter says: ``` def rgen (n): for elem in list: yield elem ``` When I call rgen(2), I would like to yield every element of that list twice etc. But how could I actually implement that?

Original source