Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8863533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:00:09+00:00 2026-06-14T16:00:09+00:00

I would like to split a list in parts, without knowing how much items

  • 0

I would like to split a list in parts, without knowing how much items I will have in that list. The question is different from those of you who wants to split a list into chunk of fixed size.

int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

I would like the values to be splitted vertically.

Splitted in 2 :

-------------------
| item 1 | item 6 |
| item 2 | item 7 |
| item 3 | item 8 |
| item 4 | item 9 |
| item 5 |        |

Splitted in 3:

| item 1 | item 4 | item 7 |
| item 2 | item 5 | item 8 |
| item 3 | item 6 | item 9 |

Splitted in 4:

| item 1 | item 4 | item 6 | item 8 |
| item 2 | item 5 | item 7 | item 9 |
| item 3 |        |        |        |

I’ve found a few c# extensions that can do that but it doesn’t distribute the value the way I want. Here’s what I’ve found:

// this technic is an horizontal distribution
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
{
    int i = 0;
    var splits = from item in list
                    group item by i++ % parts into part
                    select part.AsEnumerable();
    return splits;
}

the result is this but my problem is that the value are distributed horizontally:

| item 1 | item 2 |
| item 3 | item 4 |
| item 5 | item 6 |
| item 7 | item 8 |
| item 9 |        |

or

| item 1 | item 2 | item 3 |
| item 4 | item 5 | item 6 |
| item 7 | item 8 | item 9 |

any idea how I can distribute my values vertically and have the possibility to choose the number of parts that i want?

In real life

For those of you who want to know in which situation I would like to split a list vertically, here’s a screenshot of a section of my website:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:00:11+00:00Added an answer on June 14, 2026 at 4:00 pm

    With .Take() and .Skip() you can:

    int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
    int splitIndex = 4; // or (a.Length / 2) to split in the middle.
    
    var list1 = a.Take(splitIndex).ToArray(); // Returns a specified number of contiguous elements from the start of a sequence.
    var list2 = a.Skip(splitIndex).ToArray(); // Bypasses a specified number of elements in a sequence and then returns the remaining elements.
    

    You can use .ToList() instead of .ToArray() if you want a List<int>.


    EDIT:

    After you changed (clarified maybe) your question a bit, I guess this is what you needed:

    public static class Extensions
    {
        public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> source, int parts)
        {
            var list = new List<T>(source);
            int defaultSize = (int)((double)list.Count / (double)parts);
            int offset = list.Count % parts;
            int position = 0;
    
            for (int i = 0; i < parts; i++)
            {
                int size = defaultSize;
                if (i < offset)
                    size++; // Just add one to the size (it's enough).
    
                yield return list.GetRange(position, size);
    
                // Set the new position after creating a part list, so that it always start with position zero on the first yield return above.
                position += size;
            }
        }
    }
    

    Using it:

    int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    var lists = a.Split(2);
    

    This would generate:

    split in 2 : a.Split(2);

    | item 1 | item 6 |
    | item 2 | item 7 |
    | item 3 | item 8 |
    | item 4 | item 9 |
    | item 5 |        |
    

    split in 3 : a.Split(3);

    | item 1 | item 4 | item 7 |
    | item 2 | item 5 | item 8 |
    | item 3 | item 6 | item 9 |
    

    split in 4 : a.Split(4);

    | item 1 | item 4 | item 6 | item 8 |
    | item 2 | item 5 | item 7 | item 9 |
    | item 3 |        |        |        |
    

    Also, if you would have:

    int[] b = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // 10 items
    

    and split in 4 : b.Split(4);

    | item 1 | item 4 | item 7 | item 9 |
    | item 2 | item 5 | item 8 | item 10|
    | item 3 | item 6 |        |        |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge text file that I would like to split into multiple
I have a dataframe with one column that I would like to split into
I have a file with data in it that I would like to split
I would like to split the following string generated from a database resultset row.
I would like to split some content from an a html tag. I was
I cant figure out howto split items from my genericList to two seperate parts
I would like to split a line into words. I know this can be
I would like to split a form across several tab panels to avoid a
I am writing many files to disk, and I would like to split them
I would like to remove the configuration out of my app.js and split it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.