HOWTO · Python

How to Get Dictionary Keys as a List in Python

Get Python dictionary keys as a list with list(), loops, comprehensions, or unpacking, and learn how a dict_keys view differs from a list.

On this page

To get Python dictionary keys as a list, use list(my_dict). list(my_dict.keys()) produces the same list. The list() constructor behind both expressions collects the dictionary’s keys into a new list. The my_dict.keys() method alone returns a dict_keys view, which you can iterate but cannot index like a list. Choose a conversion when you need a list object; otherwise, iterating the dictionary directly is usually enough.

dict.keys() to Get Python Dictionary Keys

The dict.keys() method returns a view of the current keys. A view reflects later changes to the dictionary, whereas a list made from it is a separate snapshot. In Python 3.7 and later, dictionary iteration follows insertion order; the displayed order below assumes that behavior.

state_capitals = {
    "California": "Sacramento",
    "Pennsylvania": "Harrisburg",
    "Texas": "Austin",
}

keys_view = state_capitals.keys()
print(keys_view)
for state in keys_view:
    print(state)

keys_list = list(keys_view)
print(keys_list)
print(list(state_capitals))
print(keys_list[0])

state_capitals["Nevada"] = "Carson City"
print(list(keys_view))
print(keys_list)
dict_keys(['California', 'Pennsylvania', 'Texas'])
California
Pennsylvania
Texas
['California', 'Pennsylvania', 'Texas']
['California', 'Pennsylvania', 'Texas']
California
['California', 'Pennsylvania', 'Texas', 'Nevada']
['California', 'Pennsylvania', 'Texas']

Both list(keys_view) and list(state_capitals) create lists; the latter works because iterating a dictionary yields its keys. Indexing the view itself raises TypeError. If the dictionary is empty, conversion gives [], and indexing that empty list raises IndexError.

Loops to Get Python Dictionary Keys

An explicit loop is useful when you need to process each key as you collect it. Use for key in state_capitals—the in keyword is required, and the loop receives keys rather than values.

state_capitals = {
    "California": "Sacramento",
    "Pennsylvania": "Harrisburg",
    "Texas": "Austin",
}

keys = []
for key in state_capitals:
    keys.append(key)
print(keys)
['California', 'Pennsylvania', 'Texas']

For an empty dictionary, the loop runs zero times and leaves keys empty. If you only want the keys without extra work in the loop, list(state_capitals) is shorter.

List Comprehensions to Get Python Dictionary Keys

A list comprehension can express the same collection loop in one line. Keep this form when you intend to add a filter or transform each key; for a plain copy, the list constructor is simpler.

state_capitals = {
    "California": "Sacramento",
    "Pennsylvania": "Harrisburg",
    "Texas": "Austin",
}

keys = [key for key in state_capitals]
print(keys)
['California', 'Pennsylvania', 'Texas']

This comprehension also returns [] for an empty dictionary.

Python Dictionary Unpacking * Operator to Get the Keys

The iterable unpacking * syntax can insert dictionary keys into a list display: [*state_capitals]. PEP 448 generalized unpacking in Python 3.5; that historical compatibility is based on the syntax specification, not an execution here on Python 3.5. Unpacking also works in tuple and set displays.

state_capitals = {
    "California": "Sacramento",
    "Pennsylvania": "Harrisburg",
    "Texas": "Austin",
}

keys_list = [*state_capitals]
keys_tuple = (*state_capitals,)
keys_set = {*state_capitals}
print(keys_list)
print(keys_tuple)
print(sorted(keys_set))
['California', 'Pennsylvania', 'Texas']
('California', 'Pennsylvania', 'Texas')
['California', 'Pennsylvania', 'Texas']

The set removes duplicates and has no promised display order; sorted() makes its shown contents predictable. The tuple and list preserve dictionary iteration order in Python 3.7 and later. Use the list form when the result must actually be a list.

Compare Runtime Performance

If conversion speed matters, measure it with your Python interpreter and a dictionary sized like your real input. This script reports its Python version and the best of five runs, each with 10,000 conversions of the same 100-entry dictionary. Every timed statement creates a new list; the loop creates a fresh destination on each conversion.

import platform
from timeit import repeat

state_capitals = {str(number): number for number in range(100)}
methods = {
    "list(d.keys())": "list(state_capitals.keys())",
    "list(d)": "list(state_capitals)",
    "loop": "keys = []\nfor key in state_capitals:\n    keys.append(key)",
    "comprehension": "[key for key in state_capitals]",
    "unpacking": "[*state_capitals]",
}

print("Python", platform.python_version())
for name, statement in methods.items():
    times = repeat(statement, globals=globals(), number=10_000, repeat=5)
    print(f"{name}: {min(times) / 10_000:.9f} seconds per conversion")

The printed times depend on the interpreter, machine, and dictionary size, so there is no fixed output or universally fastest method. For routine code, prefer list(state_capitals) for clarity; benchmark alternatives only when conversion is a meaningful cost in your program.