C equivalent of rotating a list using python slice operation
Codemia Knowledge Hub ·
Introduction
Python list rotation is often written with slices such as items[n:] + items[:n]. In C#, there is no identical slice operator for lists, but the same result is easy to express with Skip and Take, array slicing, or an in-place rotation algorithm if performance matters.
The Python Idea
A left rotation in Python often looks like this:
That creates a new list. The key concept is:
- take the tail from position
n - append the head from the start up to
n
The same idea maps cleanly to C#.
C# Equivalent with LINQ
For most application code, the direct translation is:
This is the most readable answer if you want a new rotated list and performance is not extremely sensitive.
Normalize the Rotation Amount
A rotation count can be larger than the list size or even negative, depending on how the caller defines the operation. Normalize it first.
That formula keeps n in the range from zero to count minus one.
Arrays and Ranges
If you are working with arrays in recent C#, ranges can make the code feel closer to Python.
This is concise, but remember it still creates new arrays and copies data.
In-Place Rotation for Performance
If the list is large and you want to avoid allocating a new collection, use an in-place algorithm. A classic method is the three-reversal technique for arrays.
This mutates the original array and uses constant extra space.
Choose by Intent
Use the right implementation for the actual need:
- '
SkipandTakefor readability' - ranges for concise array code
- in-place reversal for low-allocation performance
Do not start with the in-place version unless you actually need it. It is more algorithmic and less immediately readable.
Right Rotation
If you want a right rotation instead of a left rotation, convert it to an equivalent left rotation.
This keeps the main implementation model simple.
Common Pitfalls
- Forgetting to normalize the rotation amount when it is larger than the collection size.
- Using LINQ in tight loops where repeated allocations matter.
- Applying array range syntax to
List<T>and expecting identical behavior. - Writing an in-place algorithm before confirming that mutation is acceptable.
- Mixing left and right rotation semantics without documenting which one the method uses.
Summary
- The direct C# equivalent of Python slice-based rotation is usually
Skip(n).Concat(Take(n)). - For arrays, ranges such as
items[n..]anditems[..n]can be concise. - Normalize the step count before rotating.
- Use an in-place reversal algorithm only when you need lower allocation or mutation in place.
- Be explicit about whether the rotation is left or right.
Related reading
- C equivalent to Java's Exception.printStackTrace?
- C hashcode for array of ints
- C How to draw a Binary Tree to the console
- C int to byte
- Calculate the Cumulative Distribution Function CDF in Python
- Calculating a directory's size using Python?
- C Equivalent of SQL Server DataTypes
- C equivalent of the IsNull function in SQL Server
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

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 courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.