Split a List into sublists of size n in C#
This post will discuss how to split a list into sublists of size n in C#.
1. Using Enumerable.GroupBy() method
In LINQ, you can use the Enumerable.GroupBy() method to partition a list into multiple sublists. It groups the elements of a sequence according to a specified key selector function. The following code example demonstrates how the combination of Select() and GroupBy() method can break a list into the sublists according to the specified chunk size:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static List<List<T>> partition<T>(this List<T> values, int chunkSize) { return values.Select((x, i) => new { Index = i, Value = x }) .GroupBy(x => x.Index / chunkSize) .Select(x => x.Select(v => v.Value).ToList()) .ToList(); } } public class Example { public static void Main() { List<int> values = new List<int>() { 1, 2, 3, 4, 5 }; List<List<int>> partitions = values.partition(2); foreach (List<int> partition in partitions) { Console.WriteLine(String.Join(", ", partition)); } } } |
Output:
1, 2
3, 4
5
2. Using List<T>.GetRange() method
The List<T>.GetRange() method is used to get elements between the desired range from a List<T>. The following example demonstrates the usage of the List<T>.GetRange() method for splitting a List<T> into specified number of chunks of equal sizes.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static IEnumerable<List<T>> partition<T>(this List<T> values, int chunkSize) { for (int i = 0; i < values.Count; i += chunkSize) { yield return values.GetRange(i, Math.Min(chunkSize, values.Count - i)); } } } public class Example { public static void Main() { List<int> values = new List<int>() { 1, 2, 3, 4, 5 }; var partitions = values.partition(2); foreach (List<int> partition in partitions) { Console.WriteLine(String.Join(", ", partition)); } } } |
Output:
1, 2
3, 4
5
The above generic extension method returns an IEnumerable. To return a List instead, do as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Collections.Generic; public static class Extensions { public static List<List<T>> partition<T>(this List<T> values, int chunkSize) { var partitions = new List<List<T>>(); for (int i = 0; i < values.Count; i += chunkSize) { partitions.Add(values.GetRange(i, Math.Min(chunkSize, values.Count - i))); } return partitions; } } public class Example { public static void Main() { List<int> values = new List<int>() { 1, 2, 3, 4, 5 }; var partitions = values.partition(2); foreach (List<int> partition in partitions) { Console.WriteLine(String.Join(", ", partition)); } } } |
Output:
1, 2
3, 4
5
3. Using Enumerable.Take() method
Alternatively, you can use the Enumerable.Take() method to split a list of items into chunks of a specific size. The Take() method returns a specified number of contiguous elements from the start of a sequence, and can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static IEnumerable<IEnumerable<T>> partition<T>(this IEnumerable<T> values, int chunkSize) { while (values.Any()) { yield return values.Take(chunkSize).ToList(); values = values.Skip(chunkSize).ToList(); } } } public class Example { public static void Main() { List<int> values = new List<int>() { 1, 2, 3, 4, 5 }; var partitions = values.partition(2); foreach (List<int> partition in partitions) { Console.WriteLine(String.Join(", ", partition)); } } } |
Output:
1, 2
3, 4
5
That’s all about splitting a list into sublists of size n in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)