How to combine multiple lists with a custom sequence

Not sure if there is an algorithm to describe this problem, but are there any nifty methods for combining a list in a user-defined sequence. For instance:

List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();

list1.Add("a");
list1.Add("b");
list1.Add("c");

list2.Add("d");
list2.Add("e");
list2.Add("f");

list3.Add("g");
list3.Add("h");
list3.Add("i");

List<string> combined = new List<string>();

I would like the contents of the combination to contain the following sequence:

a //First record in list1
d //First record in list2
g //First record in list3
b //Second record in list1
e //Second record in list2
h //Second record in list3
c //Third record in list1 
f //Third record in list2 
i //Third record in list3 

The number of entries in each list may not be equal.

EDIT

When the number of entries in each list may not be equal, I mean:

List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();

list1.Add("a");

list2.Add("b");
list2.Add("c");

list3.Add("d");
list3.Add("e");
list3.Add("f");

List<string> combined = new List<string>();

Expected results:

a //First record in list1
b //First record in list2
d //First record in list3
c //Second record in list2
e //Second record in list3
f //Third record in list3
+5
source share
6 answers

Not sure if there is a name. Merger? Splice? But the code is simple.

var lists = new [] { list1, list2, list3 };
var combined = new List<string>(lists.Sum(l => l.Count));    

for (var i = 0; i < lists.Max(l => l.Count); i++)
{
   foreach (var list in lists)
   { 
      if (i < list.Count)
          combined.Add (list[i])
   }
}
+4
source
int MaxCount = List1.Count; //Or whatever the highest list count.

            for (int i = 0; i < MaxCount; i++)
                {
                    if( list1.Count > i)
                    combined.Add(list1[i]);

                    if( list2.Count > i)
                    combined.Add(list2[i]);

                    if( list3.Count > i)
                    combined.Add(list3[i]);
                }
+2
source

Merge<T>:

    public static IEnumerable<T> Merge<T>(params List<T>[] lists)
    {
        var max = lists.Max(list => list.Count());

        for (int i = 0; i < max; i++)
        {
            foreach (var list in lists)
            {
                if (i < list.Count)
                {
                    yield return list[i];
                }
            }
        }
    }

:

var merged = Merge(list1, list2, list3).ToList();
+1

, . .

:

List<string> result = new List<string>();
int listLength = Math.Max(list1.Count, Math.Max(list2.Count, list3.Count)); // get the largest list length
for(int index = 0; index < listLength; i++){
  if(list1.Count > index) result.Add(list1[index);
  if(list2.Count > index) result.Add(list3[index);
  if(list3.Count > index) result.Add(list3[index);
}
+1
source

LINQPad quick example:

void Main()
{
    List<string> list1 = new List<string>();
    List<string> list2 = new List<string>();
    List<string> list3 = new List<string>();

    list1.Add("a");
    list1.Add("b");
    list1.Add("c");

    list2.Add("d");
    list2.Add("e");
    list2.Add("f");

    list3.Add("g");
    list3.Add("h");
    list3.Add("i");

    Merge(new[] { list1, list2, list3}, (c1, c2) => c1 + c2).SelectMany(s => s).Dump();
}

IEnumerable<T> Merge<T>(IEnumerable<IEnumerable<T>> sources, Func<T, T, T> combine)
{
    return sources.Aggregate((s1, s2) => s1.Zip(s2, combine));
}

The result is IEnumerable<char>, but it's easy enough to convert to List<string>, if necessary.

0
source

You can use Concate () or AddRange, see this thread for more information on the difference between these methods.

-2
source

All Articles