xxxxxxxxxx
def shiftRight(lst) :
return lst[-1:] + lst[:-1]
def shiftLeft(lst) :
return lst[1:] + lst[:1]
xxxxxxxxxx
#Shifts all elements one to the right and moves end value to the start
li=li[-1:]+li[:-1]
xxxxxxxxxx
list = [0,1,2,3]
#shifted backwards (to left)
list.append(list.pop(0))
print(list)
list = [0,1,2,3]
#shifted forward (to right)
list.insert(0,list.pop())
print(list)
#Output:
[1,2,3,0]
[3,0,1,2]
xxxxxxxxxx
from collections import deque
items = deque([1, 2])
items.append(3) # deque == [1, 2, 3]
items.rotate(1) # The deque is now: [3, 1, 2]
items.rotate(-1) # Returns deque to original state: [1, 2, 3]
item = items.popleft() # deque == [2, 3]
xxxxxxxxxx
>>> import numpy
>>> a=numpy.arange(1,10) #Generate some data
>>> numpy.roll(a,1)
array([9, 1, 2, 3, 4, 5, 6, 7, 8])
>>> numpy.roll(a,-1)
array([2, 3, 4, 5, 6, 7, 8, 9, 1])
>>> numpy.roll(a,5)
array([5, 6, 7, 8, 9, 1, 2, 3, 4])
>>> numpy.roll(a,9)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])