Rotate a List in Python
Move the last k values to the front while keeping their order, with one slice expression.
- Right rotation with slicing
- Using % to handle large k
- collections.deque.rotate
Cut and swap the two pieces
The problem
Rotating a list shows up in schedules (whose turn is next), circular buffers, image tricks and interview questions. Moving items one step at a time works but is slow; slicing does it in one step.
Think of it like this
Rotating a list is like a round table where everyone moves two seats to the right. The last two people walk around to the first seats, and everyone else keeps their order. Rotating by the full number of seats changes nothing.
steps %= len(values)🔁 Moving 7 seats at a table of 5 is the same as 2values[-steps:]🚶 The last two people move to the frontvalues[:-steps]🪑 Everyone else keeps their order+🔗 Seat them together: [4, 5, 1, 2, 3]Rotating a list to the right by k moves its last k items to the front while keeping the order of all items.
Key points
if not values: return []handles an empty list, where% len(values)would divide by zero.steps %= len(values)makes large rotations wrap around.values[-steps:]is the tail that moves to the front:[4, 5].values[:-steps]is the rest:[1, 2, 3], and+joins the two pieces.
When should you use it?
- Round-robin turns and schedules.
- Circular buffers and shifting windows of data.
- Interview questions about slicing and modulo.
When not to use it
- Rotating repeatedly by one step:
collections.deque.rotatedoes it in place efficiently. - Huge lists where extra memory matters: an in-place reversal method avoids the copy.
See how it works
Click a box or move the slider to pick an index. Insert or delete to see why every later element has to shift.
Example code
def rotate_right(values, steps):
if not values:
return []
steps %= len(values)
return values[-steps:] + values[:-steps]
print(rotate_right([1, 2, 3, 4, 5], 2))
Result of running the code
[4, 5, 1, 2, 3]
Code explanation
Modulo handles rotations larger than the list. Two slices select the final k values and the remaining prefix. Time and space complexity are O(n).
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Try empty, single-item, and duplicate-value inputs.
- Print state after every step to observe the algorithm.
- Benchmark 100, 1,000, and 10,000 items, then compare another approach.
Common mistakes
Forgetting steps equal to zero
values[-0:] is the whole list, so without % the result is doubled.
return values[-steps:] + values[:-steps] # steps = 5 gives a list of 10
steps %= len(values)
return values[-steps:] + values[:-steps]
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges