Circular iteration on tuple starting from a specific index

Peter Otten __peter__ at web.de
Tue May 30 13:00:45 EDT 2017


Beppe wrote:

> hi all
> 
> I've a tuple, something like
> 
> x = ("A","B","C","D","E","F","G","H",)
> 
> I would want to iterate on all tuple's elements
> starting from a specific index

> with the difference that I would want to restart from the beginning when I
> reach the end of the tupla
> 
> 
> C
> D
> E
> F
> G
> H
> A
> B
> 
> I would want to make a circular iteration....
> 
> suggestions?

As an alternative to the options mentioned by Ian you can use a deque 
instead of the tuple:

>>> from collections import deque
>>> d = deque("ABCDEFGH")
>>> d.rotate(-2)
>>> print(*d)
C D E F G H A B





More information about the Python-list mailing list