Skip to main content
alternative
Source Link
Gareth Rees
  • 50.1k
  • 3
  • 130
  • 211

This seems kind of complicated compared to something like this:

def cycle(seq):
    "Generator that yields the elements of a non-empty sequence cyclically."
    i = 0
    while True:
        yield seq[i]
        i = (i + 1) % len(seq)

Or, as Joe Wallis points out in comments:

def cycle(seq):
    "Generator that yields the elements of a non-empty sequence cyclically."
    while True:
        yield from seq

This seems kind of complicated compared to something like this:

def cycle(seq):
    "Generator that yields the elements of a non-empty sequence cyclically."
    i = 0
    while True:
        yield seq[i]
        i = (i + 1) % len(seq)

This seems kind of complicated compared to something like this:

def cycle(seq):
    "Generator that yields the elements of a non-empty sequence cyclically."
    i = 0
    while True:
        yield seq[i]
        i = (i + 1) % len(seq)

Or, as Joe Wallis points out in comments:

def cycle(seq):
    "Generator that yields the elements of a non-empty sequence cyclically."
    while True:
        yield from seq
Source Link
Gareth Rees
  • 50.1k
  • 3
  • 130
  • 211

This seems kind of complicated compared to something like this:

def cycle(seq):
    "Generator that yields the elements of a non-empty sequence cyclically."
    i = 0
    while True:
        yield seq[i]
        i = (i + 1) % len(seq)