articles » current » dot-net » dictionary-to-list

.NET: Convert Dictionary Keys and Values to a List

How to create a list from either the keys or the values of a dictionary in C#.

This is a simple task thanks to the C# high-level language features.

Dictionary<string, int> dic = new Dictionary<string, int>();

List<string> keys_list = dic.Keys.ToList();
List<int> values_list = dic.Values.ToList();

// NOTE: using LINQ
List<string> keys_list2 = dic.Select(kv => kv.Key).ToList();
List<int> values_list2 = dic.Select(kv => kv.Value).ToList();

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.