Python Algorithms

Rotate a List in Python

Move the last k values to the front while keeping their order, with one slice expression.

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • Right rotation with slicing
  • Using % to handle large k
  • collections.deque.rotate
Rotate right by 2
1
2
3
4
5
4
5
1
2
3

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.

From a round table to code
steps %= len(values)🔁 Moving 7 seats at a table of 5 is the same as 2
values[-steps:]🚶 The last two people move to the front
values[:-steps]🪑 Everyone else keeps their order
+🔗 Seat them together: [4, 5, 1, 2, 3]
Cut the list in two pieces and swap them.
In one sentence

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

  1. if not values: return [] handles an empty list, where % len(values) would divide by zero.
  2. steps %= len(values) makes large rotations wrap around.
  3. values[-steps:] is the tail that moves to the front: [4, 5].
  4. 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.rotate does 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

Run code →
main.py
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))
Get a Run button for this example

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.

  1. Try empty, single-item, and duplicate-value inputs.
  2. Print state after every step to observe the algorithm.
  3. Benchmark 100, 1,000, and 10,000 items, then compare another approach.
Run in the Python compiler →

Common mistakes

Forgetting steps equal to zero

values[-0:] is the whole list, so without % the result is doubled.

Problem
return values[-steps:] + values[:-steps]  # steps = 5 gives a list of 10
Fix
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