How to Get All Keys from a Dictionary in Python
If you want every key from a Python dictionary, use the keys() method.
This is useful when you want to:
- see which keys a dictionary has
- loop through the keys
- check for a key
- convert the keys to a list
Quick answer
person = {"name": "Ana", "age": 25, "city": "Lima"}
keys_view = person.keys()
print(keys_view)
print(list(keys_view))
Output:
dict_keys(['name', 'age', 'city'])
['name', 'age', 'city']
Use dict.keys() to get all keys. Convert it with list() if you need a regular list.
Use keys() to get all dictionary keys
Call .keys() on a dictionary:
person = {"name": "Ana", "age": 25, "city": "Lima"}
keys_view = person.keys()
print(keys_view)
Output:
dict_keys(['name', 'age', 'city'])
What this does:
person.keys()returns all keys from the dictionary- the result is a
dict_keysobject - you can print it or loop through it
This is the standard way to get dictionary keys in Python.
If you want more detail on this method, see the dict.keys() method reference.
Convert dictionary keys to a list
Beginners often expect keys() to return a list. It does not.
If you need a normal list, wrap it with list():
person = {"name": "Ana", "age": 25, "city": "Lima"}
key_list = list(person.keys())
print(key_list)
Output:
['name', 'age', 'city']
Use a list when you need list behavior, such as:
- indexing
- passing keys to code that expects a list
- printing in a more familiar format
Example:
person = {"name": "Ana", "age": 25, "city": "Lima"}
key_list = list(person.keys())
print(key_list[0])
Output:
name
Only convert to a list when you actually need it.
Loop through all keys
You can loop through dictionary keys in two common ways.
Loop directly over the dictionary
person = {"name": "Ana", "age": 25, "city": "Lima"}
for key in person:
print(key)
Output:
name
age
city
Loop with keys()
person = {"name": "Ana", "age": 25, "city": "Lima"}
for key in person.keys():
print(key)
Output:
name
age
city
Both approaches work.
In Python, looping directly over a dictionary is shorter and very common. But using keys() can be clearer when you want to show that you are working only with keys.
If you want to go deeper, see how to loop through a dictionary in Python.
When keys() is useful
keys() is helpful when:
- you only need the key names
- you want clearer code
- you want to convert keys to
list(),set(), orsorted() - you want to inspect the structure of a dictionary
Example with sorted():
person = {"name": "Ana", "age": 25, "city": "Lima"}
sorted_keys = sorted(person.keys())
print(sorted_keys)
Output:
['age', 'city', 'name']
Example with a key check:
person = {"name": "Ana", "age": 25, "city": "Lima"}
print("age" in person)
print("country" in person)
Output:
True
False
For simple key checks, Python usually uses the dictionary directly with in.
You can learn more in how to check if a key exists in a dictionary in Python.
Common mistakes
Here are some common problems beginners run into.
Using key() instead of keys()
This is incorrect:
my_dict = {"a": 1, "b": 2}
# my_dict.key()
The correct method name is keys():
my_dict = {"a": 1, "b": 2}
print(my_dict.keys())
Expecting keys() to return a list
This prints a dict_keys object, not a list:
my_dict = {"a": 1, "b": 2}
print(my_dict.keys())
If you need a list:
my_dict = {"a": 1, "b": 2}
print(list(my_dict.keys()))
Confusing keys with values
Keys are the names on the left side of the dictionary.
Values are the data stored for each key.
my_dict = {"name": "Ana", "age": 25}
print(my_dict.keys())
print(my_dict.values())
Output:
dict_keys(['name', 'age'])
dict_values(['Ana', 25])
If you need values instead, see the dict.values() method reference.
Using parentheses incorrectly with dict_keys
A dict_keys object is not called like a function.
This is wrong:
my_dict = {"a": 1, "b": 2}
keys_view = my_dict.keys()
# keys_view()
Treat it like a result you can print, loop through, or convert to a list.
Helpful debugging commands
If your code is not working, these quick checks can help:
print(my_dict)
print(my_dict.keys())
print(list(my_dict.keys()))
print(type(my_dict.keys()))
for key in my_dict:
print(key)
These help you confirm:
- what the dictionary contains
- what
keys()returns - whether you need a list
- whether your loop is reading the keys correctly
FAQ
Does keys() return a list?
No. It returns a dict_keys object. Use list(my_dict.keys()) if you need a list.
Can I loop through a dictionary without keys()?
Yes. Looping directly over a dictionary gives its keys.
my_dict = {"a": 1, "b": 2}
for key in my_dict:
print(key)
How do I get keys and values together?
Use my_dict.items() to get both keys and values.
my_dict = {"a": 1, "b": 2}
for key, value in my_dict.items():
print(key, value)
If you want to learn that method, see the dict.items() method reference.
How do I check if a key exists?
Use in with the dictionary:
my_dict = {"a": 1, "b": 2}
print("a" in my_dict)
This is the simplest way to test whether a key exists.
You can also read how to access values in a dictionary in Python if you want to use the key after checking it.