0
0
CsharpHow-ToBeginner · 2 min read

C# How to Convert Dictionary to List with Examples

You can convert a dictionary to a list in C# by using dictionary.ToList() to get a list of key-value pairs or by selecting keys or values with dictionary.Keys.ToList() or dictionary.Values.ToList().
📋

Examples

Input{"apple": 1, "banana": 2}
Output[KeyValuePair<string, int>("apple", 1), KeyValuePair<string, int>("banana", 2)]
Input{"cat": 5, "dog": 7, "bird": 3}
Output[KeyValuePair<string, int>("cat", 5), KeyValuePair<string, int>("dog", 7), KeyValuePair<string, int>("bird", 3)]
Input{}
Output[]
🧠

How to Think About It

To convert a dictionary to a list, think about what you want in the list: all key-value pairs, just keys, or just values. Then use the dictionary's built-in methods like ToList() or select keys or values and convert them to a list.
📐

Algorithm

1
Get the dictionary input.
2
Decide if you want a list of key-value pairs, keys only, or values only.
3
Use the appropriate method: <code>ToList()</code> on the dictionary, keys, or values.
4
Return the resulting list.
💻

Code

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

class Program {
    static void Main() {
        var dict = new Dictionary<string, int> {
            {"apple", 1},
            {"banana", 2}
        };

        var listPairs = dict.ToList();
        var listKeys = dict.Keys.ToList();
        var listValues = dict.Values.ToList();

        Console.WriteLine("List of KeyValuePairs:");
        foreach (var kvp in listPairs) {
            Console.WriteLine($"{kvp.Key}: {kvp.Value}");
        }

        Console.WriteLine("List of Keys:");
        listKeys.ForEach(Console.WriteLine);

        Console.WriteLine("List of Values:");
        listValues.ForEach(Console.WriteLine);
    }
}
Output
List of KeyValuePairs: apple: 1 banana: 2 List of Keys: apple banana List of Values: 1 2
🔍

Dry Run

Let's trace converting a dictionary {"apple":1, "banana":2} to a list of key-value pairs.

1

Start with dictionary

{"apple": 1, "banana": 2}

2

Call ToList() on dictionary

List contains KeyValuePair("apple", 1) and KeyValuePair("banana", 2)

3

Resulting list

[KeyValuePair("apple", 1), KeyValuePair("banana", 2)]

IterationKeyValueList Item
1apple1KeyValuePair("apple", 1)
2banana2KeyValuePair("banana", 2)
💡

Why This Works

Step 1: Dictionary to List Conversion

Using ToList() on a dictionary converts it into a list of KeyValuePair objects, preserving keys and values.

Step 2: Keys or Values Only

You can convert just the keys or just the values to a list by calling Keys.ToList() or Values.ToList() respectively.

Step 3: Resulting List Type

The resulting list type depends on what you convert: a list of KeyValuePair for the whole dictionary, or list of keys or values.

🔄

Alternative Approaches

Using LINQ Select to create custom list
csharp
var list = dict.Select(kvp => kvp.Key + ":" + kvp.Value).ToList();
Creates a list of strings combining key and value, useful for display but less structured.
Manually iterating and adding to list
csharp
var list = new List<KeyValuePair<string, int>>();
foreach(var kvp in dict) {
    list.Add(kvp);
}
More verbose but clear; useful if you want to add extra processing during conversion.

Complexity: O(n) time, O(n) space

Time Complexity

Converting a dictionary to a list requires visiting each element once, so it takes linear time proportional to the number of items.

Space Complexity

A new list is created to hold all elements, so space used grows linearly with the dictionary size.

Which Approach is Fastest?

Using ToList() is the fastest and simplest method; manual iteration is slower and more verbose.

ApproachTimeSpaceBest For
dictionary.ToList()O(n)O(n)Quick conversion to list of pairs
Keys.ToList() or Values.ToList()O(n)O(n)Getting only keys or values as list
LINQ Select with custom projectionO(n)O(n)Creating custom list formats
Manual iteration and addO(n)O(n)Adding extra processing during conversion
💡
Use dictionary.ToList() to quickly get a list of key-value pairs without extra code.
⚠️
Trying to cast the dictionary directly to a list without using ToList() causes errors.