C#
list rotation
Python slicing
programming
code translation

C equivalent of rotating a list using python slice operation

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

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:

python
1items = [1, 2, 3, 4, 5]
2n = 2
3rotated = items[n:] + items[:n]
4print(rotated)  # [3, 4, 5, 1, 2]

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:

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5List<int> items = new() { 1, 2, 3, 4, 5 };
6int n = 2;
7
8List<int> rotated = items.Skip(n).Concat(items.Take(n)).ToList();
9
10Console.WriteLine(string.Join(", ", rotated));

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.

csharp
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5List<int> items = new() { 1, 2, 3, 4, 5 };
6int n = 7;
7
8n = ((n % items.Count) + items.Count) % items.Count;
9
10List<int> rotated = items.Skip(n).Concat(items.Take(n)).ToList();
11Console.WriteLine(string.Join(", ", rotated)); // 3, 4, 5, 1, 2

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.

csharp
1using System;
2using System.Linq;
3
4int[] items = { 1, 2, 3, 4, 5 };
5int n = 2;
6
7int[] rotated = items[n..].Concat(items[..n]).ToArray();
8
9Console.WriteLine(string.Join(", ", rotated));

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.

csharp
1using System;
2
3static void Reverse(int[] array, int start, int end)
4{
5    while (start < end)
6    {
7        (array[start], array[end]) = (array[end], array[start]);
8        start++;
9        end--;
10    }
11}
12
13static void RotateLeft(int[] array, int n)
14{
15    if (array.Length == 0) return;
16
17    n = ((n % array.Length) + array.Length) % array.Length;
18    if (n == 0) return;
19
20    Reverse(array, 0, n - 1);
21    Reverse(array, n, array.Length - 1);
22    Reverse(array, 0, array.Length - 1);
23}
24
25int[] values = { 1, 2, 3, 4, 5 };
26RotateLeft(values, 2);
27Console.WriteLine(string.Join(", ", values));

This mutates the original array and uses constant extra space.

Choose by Intent

Use the right implementation for the actual need:

  • 'Skip and Take for 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.

csharp
int rightSteps = 1;
int leftSteps = items.Count - (rightSteps % items.Count);
List<int> rotated = items.Skip(leftSteps).Concat(items.Take(leftSteps)).ToList();

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..] and items[..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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.