0
0
PythonHow-ToBeginner · 2 min read

Python How to Convert Dictionary to List Easily

You can convert a dictionary to a list in Python by using 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

Input{'a': 1, 'b': 2}
Output['a', 'b']
Input{'x': 10, 'y': 20}
Output[10, 20]
Input{'name': 'Alice', 'age': 30}
Output[('name', 'Alice'), ('age', 30)]
🧠

How to Think About It

To convert a dictionary to a list, decide what part you want: keys, values, or both. Use list() on the dictionary to get keys, on .values() to get values, or on .items() to get key-value pairs as tuples.
📐

Algorithm

1
Get the dictionary input.
2
Choose whether to convert keys, values, or items.
3
Apply <code>list()</code> to the chosen dictionary view.
4
Return the resulting list.
💻

Code

python
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)
Output
['a', 'b'] [1, 2] [('a', 1), ('b', 2)]
🔍

Dry Run

Let's trace converting {'a': 1, 'b': 2} to a list of keys.

1

Start with dictionary

{'a': 1, 'b': 2}

2

Apply list() to dictionary

list({'a': 1, 'b': 2})

3

Result is list of keys

['a', 'b']

StepOperationResult
1Input dictionary{'a': 1, 'b': 2}
2list(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

List comprehension for keys
python
my_dict = {'a': 1, 'b': 2}
keys_list = [key for key in my_dict]
print(keys_list)
More explicit but less concise than list(my_dict).
List comprehension for values
python
my_dict = {'a': 1, 'b': 2}
values_list = [my_dict[key] for key in my_dict]
print(values_list)
Works similarly but uses explicit iteration.
Using unpacking with * operator
python
my_dict = {'a': 1, 'b': 2}
keys_list = [*my_dict]
print(keys_list)
Unpacks keys into a list; concise and readable.

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.

ApproachTimeSpaceBest 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 comprehensionO(n)O(n)Custom processing
Unpacking with *O(n)O(n)Concise keys list
💡
Use list(your_dict) to quickly get all keys as a list.
⚠️
Trying to convert a dictionary directly to a list expecting values or items instead of keys.