How to Get All the Keys of a Dictionary in Python with keys()
A dictionary pairs keys with values, and sometimes you only want the keys: the names on a price list, the words a table has entries for. keys() gives you all of them at once, in the order they were added. This guide gets them and turns them into a list on Python 3.15.2.
Syntax
<keys> = <dictionary>.keys()
<key_list> = list(<dictionary>.keys())
| Part | What it means |
|---|---|
<dictionary> | Any dictionary |
.keys() | Returns a view of the keys, in the dictionary's order |
list(...) | Copies the keys into a plain list, which you can read by position |
The built-in types reference says keys() will "Return a new view of the dictionary's keys."
The Example
A café's price list maps each item to its price: tea, cake and juice. The script prints the keys, then the keys as a list.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
prices = {"tea": 2.5, "cake": 4.0, "juice": 3.0}
print(prices.keys())
print(list(prices.keys()))

Step 2: Run It
uv run python prices.py
dict_keys(['tea', 'cake', 'juice'])
['tea', 'cake', 'juice']

keys() returned a dict_keys view of tea, cake and juice, in the order they were written in the dictionary. list copied them into a plain list. The command works unchanged in PowerShell.
A View, Not a Copy
The dict_keys object isn't a list of its own. It looks at the dictionary, so it follows every change. The reference says views "provide a dynamic view on the dictionary's entries, which means that when the dictionary changes, the view reflects these changes."
To see that, I saved the view with keys = prices.keys(), added a scone to the dictionary with prices["scone"] = 3.5, and printed keys again. It showed dict_keys(['tea', 'cake', 'juice', 'scone']), although I never called keys() a second time. A list made with list(prices.keys()) is a copy: one made before I added a bagel still ended with 'scone' afterwards.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Reading a key by position from the view | prices.keys()[0] raised TypeError: 'dict_keys' object is not subscriptable | Make a list first: list(prices.keys())[0] gave 'tea' |
Adding keys while looping over keys() | A loop over prices.keys() that added a large size for each item raised RuntimeError: dictionary changed size during iteration | Loop over a copy: for name in list(prices.keys()): ran to the end and added all three |
FAQ
In what order do the keys come?
In the order they were added. The reference says: "Dictionaries preserve insertion order." For another order, sort them: sorted(prices.keys()) gave ['cake', 'juice', 'tea'].
Is there a shorter way to get the list?
Yes: list(prices) gave the same ['tea', 'cake', 'juice']. The tutorial says "Performing list(d) on a dictionary returns a list of all the keys used in the dictionary, in insertion order". Writing .keys() makes it plainer to a reader that you want the keys.
How do I check whether a key exists?
With in, and you don't need keys() for it. "tea" in prices gave True and "scone" in prices gave False. "tea" in prices.keys() gave True as well.
How many keys does a dictionary have?
len(prices.keys()) gave 3. An empty dictionary has an empty view: {}.keys() gave dict_keys([]).
Conclusion
prices.keys() returned a view of the three keys, tea, cake and juice, and list(prices.keys()) copied them into a list. The view follows the dictionary, and the list is a copy that stays as it was.
Additional Resources
- dict.keys() — the method.
- Dictionary view objects — what a view is and what it can do.
- Dictionaries — the tutorial, with
list(d)andin.
