Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map two lists into a dictionary in C#

Given two IEnumerables of the same size, how can I convert it to a Dictionary using Linq?

IEnumerable<string> keys = new List<string>() { "A", "B", "C" };
IEnumerable<string> values = new List<string>() { "Val A", "Val B", "Val C" };

var dictionary = /* Linq ? */;

And the expected output is:

A: Val A
B: Val B
C: Val C

I wonder if there is some simple way to achieve it.

And should I be worried about performance? What if I have large collections?


I don't if there is an easier way to do it, currently I'm doing like this:

I have an Extension method that will loop the IEnumerable providing me the element and the index number.

public static class Ext
{
    public static void Each<T>(this IEnumerable els, Action<T, int> a)
    {
        int i = 0;
        foreach (T e in els)
        {
            a(e, i++);
        }
    }
}

And I have a method that will loop one of the Enumerables and with the index retrieve the equivalent element on the other Enumerable.

public static Dictionary<TKey, TValue> Merge<TKey, TValue>(IEnumerable<TKey> keys, IEnumerable<TValue> values)
{
    var dic = new Dictionary<TKey, TValue>();

    keys.Each<TKey>((x, i) =>
    {
        dic.Add(x, values.ElementAt(i));
    });

    return dic;
}

Then I use it like:

IEnumerable<string> keys = new List<string>() { "A", "B", "C" };
IEnumerable<string> values = new List<string>() { "Val A", "Val B", "Val C" };

var dic = Util.Merge(keys, values);

And the output is correct:

A: Val A
B: Val B
C: Val C
like image 982
BrunoLM Avatar asked Oct 28 '10 01:10

BrunoLM


People also ask

How do I convert a nested list to a dictionary?

Converting a Nested List to a Dictionary Using Dictionary Comprehension. We can convert a nested list to a dictionary by using dictionary comprehension. It will iterate through the list. It will take the item at index 0 as key and index 1 as value.

Can a dictionary have 2 values?

In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.

How do I map two names into a dictionary?

Map two lists into a dictionary using zip() function To map two lists together, we can use the Python zip() function. This function allows us to combine two lists together. We can use one list as the keys for the dictionary and the other as the values.


2 Answers

Or based on your idea, LINQ includes an overload of Select() that provides the index. Combined with the fact that values supports access by index, one could do the following:

var dic = keys.Select((k, i) => new { k, v = values[i] })
              .ToDictionary(x => x.k, x => x.v);

(If values is kept as List<string>, that is...)

like image 117
dahlbyk Avatar answered Oct 08 '22 11:10

dahlbyk


I like this approach:

var dict =
   Enumerable.Range(0, keys.Length).ToDictionary(i => keys[i], i => values[i]);
like image 14
Todd Menier Avatar answered Oct 08 '22 09:10

Todd Menier