Slicing is very helpful:
def moveRight(mylist, distance=1, pad=0):
return ([pad] * distance) + mylist[:0 - distance]
def moveLeft(mylist, distance=1, pad=0):
return mylist[distance:] + ([pad] * distance)
You could also do the change in-place:
def moveRight(mylist, distance=1, pad=0):
mylist[:] = ([pad] * distance) + mylist[:0 - distance]
def moveLeft(mylist, distance=1, pad=0):
mylist[:] = mylist[distance:] + ([pad] * distance)
Both ways, the default is to shift by one, but you could shift by any positive integer. You can also use something other than 0
to fill in the missing elements with.