C# – Convert a dictionary to a list

The simplest way to convert a dictionary to a list is by using the Linq ToList() method, like this:

using System.Collections.Generic;
using System.Linq;

var dictionary = new Dictionary<int, string>()
{
    [100] = "Bob",
    [101] = "Linda",
    [102] = "Teddy"
};

var list = dictionary.ToList(); //returns List<KeyValuePair<int, string>>

foreach(var kvp in list)
{
    Console.WriteLine($"{kvp.Key}={kvp.Value}");
}
Code language: C# (cs)

When used on a dictionary, ToList() returns a list of KeyValuePair objects. This example outputs the following:

100=Bob
101=Linda
102=TeddyCode language: plaintext (plaintext)

Dictionary keys to list

If you want a list of the dictionary’s keys, use ToList() on Dictionary.Keys, like this:

using System.Collections.Generic;
using System.Linq;

var dictionary = new Dictionary<int, string>()
{
    [100] = "Bob",
    [101] = "Linda",
    [102] = "Teddy"
};

var list = dictionary.Keys.ToList(); //returns List<int>

foreach(var key in list)
{
    Console.WriteLine(key);
}
Code language: C# (cs)

Using ToList() on the keys returns a list of the key type. In this example, the dictionary’s keys are integers, so this returns a list of integers. This outputs the following:

100
101
102Code language: plaintext (plaintext)

Dictionary values to list

If you want a list of the dictionary’s values, use ToList() on Dictionary.Values, like this:

using System.Collections.Generic;
using System.Linq;

var dictionary = new Dictionary<int, string>()
{
    [100] = "Bob",
    [101] = "Linda",
    [102] = "Teddy"
};

var list = dictionary.Values.ToList(); //returns List<string>

foreach(var val in list)
{
    Console.WriteLine(val);
}
Code language: C# (cs)

Using ToList() on the values returns a list of the value type, which is string in this example. This example outputs the following:

Bob
Linda
TeddyCode language: plaintext (plaintext)

Dictionary to list of tuples

When you want to convert the dictionary to a list of tuples, use Select() (Linq) to create a named tuple for each key/value pair, then use ToList(). Here’s an example:

using System.Collections.Generic;
using System.Linq;

var dictionary = new Dictionary<int, string>()
{
    [100] = "Bob",
    [101] = "Linda",
    [102] = "Teddy"
};

var list = dictionary.Select(kvp => (Id: kvp.Key, Name: kvp.Value)).ToList();

foreach(var tuple in list)
{
    Console.WriteLine($"{tuple.Name} has id {tuple.Id}");
}
Code language: C# (cs)

This outputs the following:

Bob has id 100
Linda has id 101
Teddy has id 102Code language: plaintext (plaintext)

With a loop

ToList() is a convenience method. Internally, it passes the dictionary to the List constructor, which then does all the work of adding the dictionary’s items to the list. When you want more control over the process (or don’t want to use Linq methods), you can create the list, loop through the dictionary, and add items to the list. Here’s an example:

using System.Collections.Generic;

var dictionary = new Dictionary<int, string>()
{
    [100] = "Bob",
    [101] = "Linda",
    [102] = "Teddy"
};


var list = new List<KeyValuePair<int, string>>();

foreach(var kvp in dictionary)
{
    if (kvp.Key > 100)
    {
        list.Add(kvp);
    }
}

Console.WriteLine($"List has {list.Count} items");
Code language: C# (cs)

This example adds a few of the items to the list and outputs the following:

List has 2 itemsCode language: plaintext (plaintext)

Leave a Comment