How to convert List<String> to Dictionary<int,String>

34,856

Solution 1

var dict = list.Select((s, i) => new { s, i }).ToDictionary(x => x.i, x => x.s);

Solution 2

I find this to be the neatest

int index = 0;
var dictionary = myList.ToDictionary(item => index++);

Solution 3

In my opinion, what you have is more readable than the Linq way (and as a bonus, it happens to be more efficient):

foreach(var item in list)
    dictionary[index++] = item;

Solution 4

Use:

var dict = list.Select((x, i) => new {x, i})
    .ToDictionary(a => a.i, a => a.x);
Share:
34,856

Related videos on Youtube

testCoder
Author by

testCoder

Updated on March 02, 2020

Comments

  • testCoder
    testCoder about 4 years

    I have List<String>, i need to convert it to Dictionary<int,String> with auto generation of Key, is any shortest way to accomplish that? I have tried:

        var dictionary = new Dictionary<int, String>();
        int index = 0;
        list.ForEach(x=>{
          definitions.Add(index, x);
          index++;
    });
    

    but i think it is dirty way.

  • zak
    zak over 4 years
    this starts the key from 0; what if I wanted to start from any other number, like 10 and also increment by 10 not 1;
  • L.B
    L.B over 4 years
    @zak what did you get when you used Skip and Where ?
  • Rod
    Rod over 4 years
    What if the list has duplicate keys and I just want to ignore adding the duplicate to dictionary?
  • jbyrd
    jbyrd about 4 years
    Boy this is a lot simpler than the accepted answer - why make it so complex just to get it into a single line?
  • Jan Van Overbeke
    Jan Van Overbeke about 4 years
    slight variation, using unnamed tuples (C# 7): var dict = list.Select((s, i) => (s, i)).ToDictionary(x => x.i, x => x.s);
  • Mong Zhu
    Mong Zhu over 2 years
    there is a hidden pitfall when using the assignment operator like this: dictionary[index++] = item; existing keys will be silently overriden! When using the Add method it will throw an exception that the key already exists and at least notify the programmer somehow at runtime. It always depends on the usecase ;) as usual
  • Eren Ersönmez
    Eren Ersönmez over 2 years
    @MongZhu I wouldn't call it hidden or silent. When we say dict[key1] = value1, if key1 already exists, the element will be overwritten. If it doesn't exist, a new element will be created. That's how dictionary normally works, and it's documented. In this case here, the dictionary is a new empty instance, so there are no existing keys.
  • Mong Zhu
    Mong Zhu over 2 years
    I know that it is documented. After my collegue fell into this trap we read the documentation ;) but only after that :D "the dictionary is a new empty instance, so there are no existing keys" yes this is what I meant with depending on the use case. in this case it is a very valid approach
  • Eren Ersönmez
    Eren Ersönmez over 2 years
    @MongZhu you're right, something being documented doesn't always mean it's obvious. I guess we'll just leave the documentation link here (above), and hopefully it helps someone else :)