Skip to content

Lists · routine

Rotate a list

Write rotate(items, k) returning a new list with the first k items moved to the end. rotate([1, 2, 3, 4], 1) is [2, 3, 4, 1]. k may be larger than the list, and the list may be empty.

Run adds print(rotate([1, 2, 3], 5)) after your code, to try it.

Stuck on the idea rather than the code? The Comprehensions and slices lesson walks through it.