Python How to Convert Dictionary to List Easily
list(your_dict) to get the keys, list(your_dict.values()) for values, or list(your_dict.items()) for key-value pairs as tuples.Examples
How to Think About It
list() on the dictionary to get keys, on .values() to get values, or on .items() to get key-value pairs as tuples.Algorithm
Code
my_dict = {'a': 1, 'b': 2}
# Convert to list of keys
keys_list = list(my_dict)
print(keys_list)
# Convert to list of values
values_list = list(my_dict.values())
print(values_list)
# Convert to list of key-value pairs
items_list = list(my_dict.items())
print(items_list)Dry Run
Let's trace converting {'a': 1, 'b': 2} to a list of keys.
Start with dictionary
{'a': 1, 'b': 2}
Apply list() to dictionary
list({'a': 1, 'b': 2})
Result is list of keys
['a', 'b']
| Step | Operation | Result |
|---|---|---|
| 1 | Input dictionary | {'a': 1, 'b': 2} |
| 2 | list(my_dict) | ['a', 'b'] |
Why This Works
Step 1: Dictionary keys are iterable
A dictionary in Python can be directly converted to a list of its keys using list() because iterating a dictionary yields its keys.
Step 2: Values and items views
Using .values() or .items() returns views that can also be converted to lists to get values or key-value pairs.
Step 3: Resulting list types
The list of keys or values contains simple elements, while the list of items contains tuples pairing each key with its value.
Alternative Approaches
my_dict = {'a': 1, 'b': 2}
keys_list = [key for key in my_dict]
print(keys_list)my_dict = {'a': 1, 'b': 2}
values_list = [my_dict[key] for key in my_dict]
print(values_list)my_dict = {'a': 1, 'b': 2}
keys_list = [*my_dict]
print(keys_list)Complexity: O(n) time, O(n) space
Time Complexity
Converting a dictionary to a list requires visiting each key, value, or item once, so it takes linear time proportional to the number of elements.
Space Complexity
The new list stores all elements, so it uses linear extra space equal to the number of dictionary entries.
Which Approach is Fastest?
Using list() on dictionary views is the fastest and most readable; list comprehensions add overhead but offer flexibility.
| Approach | Time | Space | Best For |
|---|---|---|---|
| list(dict) | O(n) | O(n) | Quick keys list |
| list(dict.values()) | O(n) | O(n) | Quick values list |
| list(dict.items()) | O(n) | O(n) | Key-value pairs |
| List comprehension | O(n) | O(n) | Custom processing |
| Unpacking with * | O(n) | O(n) | Concise keys list |
list(your_dict) to quickly get all keys as a list.