Back to Practice
#0092
Rotate a List Right
EasyPython15 min10 XP
Problem
Given a list and integer k, return a new list rotated right by k positions. If the list is empty, return an empty list.
Why This Matters
List rotation checks whether you understand indexes, modulo, and slicing without mutating caller data accidentally.
Function Signature
def rotate_right(items, k):
Examples
Example 1
Input[1, 2, 3, 4, 5], k = 2
Output[4, 5, 1, 2, 3]
The last two values move to the front.
Constraints
- Return a new list.
- k can be larger than the list length.
- Handle empty lists.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input[1, 2, 3, 4, 5], k = 2
Expected[4, 5, 1, 2, 3]
The last two values move to the front.
Hidden Test Categories
k equals 0k larger than lengthempty list