Question: OK so I want to create within a loop iterator in index that goes like this

```
[0,1,2,3,4,5,6,7,8][1,2,3,4,5,6,7,8,0][2,3,4,5,6,7,8,0,1][3,4,5,6,7,8,0,1,2]...[8,0,1,2,3,4,5,6,7]

```

where the maximum value may be 8 or another number.

so far I have some code that doesn't work.

```
a = ['l','m','n','o','p','q','r','s','t'] #len(a) = 9
b = [[]] *len(a)
c = [[]] *len(a)
for offset_index in range(len(a)):
    b[offset_index] = []
    c[offset_index] = []
    for i in range(offset_index, len(a) - offset_index, 1):
        b[offset_index].append(i)
        c[offset_index].append(a[i])
print b

```

> 
> [[0, 1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6], [3, 4, 5], [4], [], [], [], []]
> 
> 
> 

I can see it's a problem with the 2nd range function, but can't imagine a neat fix.

Answer: 
Here is my code:

```
a = [1, 2, 3, 4, 5]
for slice_index in range(0, len(a)):
    print (a[slice_index::] + a[:slice_index:])

```

> 
> [1, 2, 3, 4, 5]  
> 
>  [2, 3, 4, 5, 1]  
> 
>  [3, 4, 5, 1, 2]  
> 
>  [4, 5, 1, 2, 3]  
> 
>  [5, 1, 2, 3, 4] 
> 
> 
> 

**Explanation:** `a[slice_index::]` gives the part of array where index >= slice\_index. Same for the other.
[`more_itertools.circular_shifts`](https://more-itertools.readthedocs.io/en/latest/api.html#more_itertools.circular_shifts) implements [circular shifts](https://en.wikipedia.org/wiki/Circular_shift), a type of [cyclic permutation](https://en.wikipedia.org/wiki/Cyclic_permutation).

**Code**

```
import more_itertools as mit

iterable = range(9)

# Option 1
mit.circular_shifts(iterable)

```

Output

```
[(0, 1, 2, 3, 4, 5, 6, 7, 8),
 (1, 2, 3, 4, 5, 6, 7, 8, 0),
 (2, 3, 4, 5, 6, 7, 8, 0, 1),
 (3, 4, 5, 6, 7, 8, 0, 1, 2),
 (4, 5, 6, 7, 8, 0, 1, 2, 3),
 (5, 6, 7, 8, 0, 1, 2, 3, 4),
 (6, 7, 8, 0, 1, 2, 3, 4, 5),
 (7, 8, 0, 1, 2, 3, 4, 5, 6),
 (8, 0, 1, 2, 3, 4, 5, 6, 7)]

```

---

**Alternatives**

Using [sliding windows](https://stackoverflow.com/questions/8269916/what-is-sliding-window-algorithm-examples) via the same [third-party library](https://github.com/erikrose/more-itertools):

```
import itertools as it

# Option 2
list(it.islice(mit.windowed(it.cycle(iterable), n=len(iterable)), len(iterable)))

# Option 3
list(mit.windowed(mit.ncycles(iterable, n=2), n=len(iterable)))[:-1]

```