>>> l=[1,2,3,4] >>> l[1:]+l[:1] [2, 3, 4, 1] >>> l=[1,2,3,4] >>> l[2:]+l[:2] [3, 4, 1, 2] >>> l[-1:]+l[:-1] [4, 1, 2, 3]
General rotation of n left (positive y in the call to rotate ) or right (negative y), then:
def rotate(l, y=1): if len(l) == 0: return l y = y % len(l)
If you want the rotation direction to be the same as your example, just rotate y in rotation.
def rotate(l, y=1): if len(l) == 0: return l y = -y % len(l)
the wolf Feb 26 2018-12-12T00: 00Z
source share