Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most efficient way to combine two List(Of String)?

Let's say I've got:

Dim los1 as New List(Of String)
los1.Add("Some value")

Dim los2 as New List(Of String)
los2.Add("More values")

What would be the most efficient way to combine the two into a single List(Of String)?

Edit: While I love the solutions everyone has provided, I probably should have also mentioned I'm stuck using the .NET 2.0 framework.

like image 821
Jason Towne Avatar asked Mar 16 '10 19:03

Jason Towne


People also ask

How do you combine two lists in python?

One simple and popular way to merge(join) two lists in Python is using the in-built append() method of python. The append() method in python adds a single item to the existing list. It doesn't return a new list of items, instead, it modifies the original list by adding the item to the end of the list.

How do I combine multiple lists into one list?

Join / Merge two lists in python using + operator In python, we can use the + operator to merge the contents of two lists into a new list. For example, We can use + operator to merge two lists i.e. It returned a new concatenated lists, which contains the contents of both list_1 and list_2.


4 Answers

I think

los1.AddRange(los2)
like image 195
Javier Avatar answered Oct 29 '22 14:10

Javier


If you just want to see a sequence of the two lists then use the Enumerable.Concat method.

Dim combined = los1.Concat(los2)

This will return an IEnemurable(Of String) which contains all of the elements in los1 and los2. It won't allocate a huge new collection but instead will iterate over the 2 separate collections.

like image 40
JaredPar Avatar answered Oct 29 '22 14:10

JaredPar


JaredPar's answer will give you an object that will enumerate over both lists. If you actually want a List(Of String) object containing these values, it's as simple as:

Dim combined As New List(Of String)(los1.Concat(los2));

EDIT: You know, just because you're using .NET 2.0 doesn't mean you can't roll your own versions of some of the LINQ extension methods you personally find useful. The Concat method in particular would be quite trivial to implement in C#*:

public static class EnumerableUtils {
    public static IEnumerable<T> Concat<T>(IEnumerable<T> first, IEnumerable<T> second) {
        foreach (T item in first)
            yield return item;

        foreach (T item in second)
            yield return item;
    }
}

Then, see here:

Dim los1 as New List(Of String)
los1.Add("Some value")

Dim los2 as New List(Of String)
los2.Add("More values")

Dim combined As New List(Of String)(EnumerableUtils.Concat(los2, los2))

* To be fair, this is a lot more straightforward in C# thanks to the yield keyword. It could be done in VB.NET, but it'd be trickier to provide deferred execution in the same manner that the LINQ extensions do.

like image 28
Dan Tao Avatar answered Oct 29 '22 12:10

Dan Tao


Union() potentially, if you want a distinct list of entries (I beleive it only does a distinct), is another alternative.

like image 41
Brian Mains Avatar answered Oct 29 '22 14:10

Brian Mains