Rotate a list by k

Rotating [1, 2, 3, 4, 5] right by 2 gives [4, 5, 1, 2, 3]. It can be done in place with three reversals: reverse the whole list, then reverse the first k items, then reverse the rest. On the example, the list goes [5, 4, 3, 2, 1], then [4, 5, 3, 2, 1], then [4, 5, 1, 2, 3].

Write rotate(nums, k) that rotates the list right by k in place and returns it. k may be larger than the list; rotating by the length brings it back to the start, so only k % len(nums) matters. The point is to practise the three reversals rather than build a new list with slicing.

rotate([1, 2, 3, 4, 5], 2)  ->  [4, 5, 1, 2, 3]
rotate([1, 2, 3], 4)        ->  [3, 1, 2]
def rotate(nums, k):
    ...