Python iterating from the middle of a cycle to before the start
<p>I've been working on programming a board game to practice python. I use a cycle to do turn order like this:</p>

<pre><code>turncycle = [0,1,2,3]
for turnindex in cycle(turncycle):
    #.
    #...turn stuff
    #...turnindex is used for active player
    #.
</code></pre>

<p>What I want to do is given a turn index start a mini turn where an event-card triggers and they have to something. Is there a way to rebuild the list so I can change [0,1,2,3] into [1,2,3,0] or cycle starting from 1,2, or 3 and then cycle through the rest once?</p>

<p>Cobbled together something from the <a href="https://docs.python.org/2.7/library/itertools.html#recipes" rel="nofollow">itertools recipes</a></p>

<pre><code>import itertools
a = [0,1,2,3,4,5,6,7]
def consume(iterator, n):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(itertools.islice(iterator, n, n), None)

def cycle(turncycle, start = 0):
    # limit to the original number of turns
    no_of_turns = len(turncycle)
    # make a non-ending cycle
    turncycle = itertools.cycle(turncycle)
    # advance to the start position
    consume(turncycle, start)
    # return a new turn cycle - from the itertools take() recipe
    return itertools.islice(turncycle, no_of_turns)

&gt;&gt;&gt; 
&gt;&gt;&gt; for n in cycle(a):
    print(n),

0 1 2 3 4 5 6 7
&gt;&gt;&gt; for n in cycle(a, 4):
    print(n),

4 5 6 7 0 1 2 3
&gt;&gt;&gt; for n in cycle(a, 20):
    print(n),

4 5 6 7 0 1 2 3
&gt;&gt;&gt;
</code></pre>

<hr>

<p><code>cycle</code> may need to be tweaked a bit depending on what you think the <code>start</code> parameter means.</p>

<p>It could probably be renamed <code>roll()</code>.</p>

python list iteration cycle