Python - Sequence Shufflers: range and len

Introduction

The range's integers provide a repeat count in the first, and a position for slicing in the second:

Demo

S = 'test' 
for i in range(len(S)):       # For repeat counts 0..3 
    S = S[1:] + S[:1]         # Move front item to end 
    print(S, end=' ') 


S = 'test' # from  w  w w . j  a va 2  s  . co  m
for i in range(len(S)):       # For positions 0..3 
    X = S[i:] + S[:i]         # Rear part + front part 
    print(X, end=' ')

Result

The second creates the same results as the first in a different order.

Demo

L = [1, 2, 3] 
for i in range(len(L)): 
     X = L[i:] + L[:i]         # Works on any sequence type 
     print(X, end=' ')

Result

Related Topic