Skip to content

Home

Rotate list elements

Moves the specified amount of elements to the start of the list.

def roll(lst, offset):
  return lst[-offset:] + lst[:-offset]

roll([1, 2, 3, 4, 5], 2) # [4, 5, 1, 2, 3]
roll([1, 2, 3, 4, 5], -2) # [3, 4, 5, 1, 2]

More like this

Start typing a keyphrase to see matching snippets.