Efficient way to rotate a list in python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Rotating a list means shifting its elements left or right and wrapping the overflow back around to the other side. In Python, the most efficient approach depends on whether you need a plain list result, in-place mutation, or repeated rotations.
For many everyday cases, slicing is the cleanest answer. If you rotate often, collections.deque is usually the better data structure because rotation is built in.
Use Slicing for a Simple One-Off Rotation
If you want a rotated copy of a list, slicing is compact and fast enough for most uses:
For left rotation, flip the slices:
This is usually the most readable solution when you only need the rotated result once and creating a new list is acceptable.
deque.rotate Is Better for Repeated Rotations
If rotation is a frequent operation, deque is the more appropriate tool:
This is especially useful when the collection is conceptually circular, such as:
- round-robin scheduling
- rotating turn order
- cyclic buffers
If your code rotates often, converting the data model to deque is usually more efficient than repeatedly rebuilding lists with slices.
In-Place Rotation Is Trickier
If the requirement is to mutate the original list in place, slicing assignment is a practical Python solution:
This still creates intermediate slices, but it preserves the original list object, which can matter if other references point to it.
That is often the right compromise in Python. A fully manual swap-based rotation algorithm is possible, but it is usually less readable and not obviously better in normal application code.
Normalize the Rotation Count
Always reduce k modulo the list length:
That handles cases such as:
- '
klarger than the list size' - multiple full turns
- cleaner boundary behavior
Without normalization, rotations such as k = 1_000_000 on a five-element list do more work than needed or require extra logic.
Choose the Method by Use Case
A simple rule of thumb works well:
- use slicing for a one-off rotated copy
- use slicing assignment for in-place mutation
- use
dequefor repeated rotations
This is better than searching for one universally "most efficient" answer, because the best choice depends on how the list is used after the rotation.
Common Pitfalls
- Forgetting to normalize
kwith modulo, which makes large rotation counts awkward. - Using list slicing inside a tight loop when a
dequewould fit the repeated-rotation workload better. - Rebinding the list to a rotated copy when callers expected the original list object to be mutated.
- Writing a complicated manual algorithm when simple slicing would be clearer and fast enough.
- Not handling the empty-list case, which leads to division by zero during modulo.
Summary
- For one-off list rotation, slicing is usually the cleanest Python solution.
- For repeated rotation operations,
collections.dequeis often the better data structure. - Use slicing assignment if you must preserve the original list object.
- Normalize the rotation count with modulo before rotating.
- Pick the method that matches the workload instead of chasing one abstract "best" algorithm.
Related reading
- Efficient way to search a stream for a string
- Efficient way to search an element
- Efficient way to store millions of arrays, and perform IN check
- Efficient ways to sort a deck of actual cards
- Efficient way to union two list with list or None value
- Efficiently convert edge list to adjacency list using MapReduce
- Efficiently finding duplicates in a list
- Efficiently grouping a list of coordinates points by location in Python

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.