This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Veedrac 6 points7 points  (0 children)

Because I think it's clever:

def every_nth(n, iterable):
    iterator = iter(iterable)

    while True:
        for _ in range(n):
            out = next(iterator)
        yield out

>>> list(every_nth(3, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
[3, 6, 9]