Python Dictionary keys() Method

The Python dictionary keys() method returns all keys from a dictionary.

It is useful when you want to:

  • view the available keys
  • loop through dictionary keys
  • check what fields exist in a dictionary

keys() does not return a regular list. It returns a special object called dict_keys.

Quick example #

student = {"name": "Ana", "age": 20}
keys = student.keys()

print(keys)
print(list(keys))

Output:

dict_keys(['name', 'age'])
['name', 'age']

Use keys() to get a view of all dictionary keys. Convert it to a list if you want a regular list output.

What keys() does #

keys() is a dictionary method that returns all keys from a dictionary.

Important points:

  • It returns all keys from a dictionary
  • It does not need any arguments
  • It returns a dict_keys view object, not a list
  • The view updates if the dictionary changes

Example:

person = {"name": "Liam", "age": 25}

print(person.keys())

person["city"] = "Paris"
print(person.keys())

Output:

dict_keys(['name', 'age'])
dict_keys(['name', 'age', 'city'])

Notice that the result updates after the dictionary changes.

Basic example #

Here is a simple example with a small dictionary:

data = {"name": "Ana", "age": 20, "grade": "A"}

print(data.keys())
print(list(data.keys()))

Output:

dict_keys(['name', 'age', 'grade'])
['name', 'age', 'grade']

This shows two things:

  • data.keys() returns a dict_keys object
  • list(data.keys()) converts it into a regular list

If you are new to dictionaries, see how to create a dictionary in Python.

Syntax #

The syntax is:

my_dict.keys()

Things to remember:

  • It works only on dictionaries
  • It takes no parameters
  • You must include the parentheses

Correct:

student = {"name": "Ana"}
print(student.keys())

Incorrect:

student = {"name": "Ana"}
print(student.keys)

Without parentheses, you are referring to the method itself, not calling it.

Return value #

keys() returns a dict_keys object.

This object:

  • contains all keys in the dictionary
  • can be used in a loop
  • can be converted with list()
  • is useful when checking available keys

Example:

car = {"brand": "Toyota", "year": 2020}

result = car.keys()

print(result)
print(type(result))

Output:

dict_keys(['brand', 'year'])
<class 'dict_keys'>

If you want dictionary values instead, see the Python dictionary values() method. If you want both keys and values together, see the Python dictionary items() method.

Looping through dictionary keys #

You can loop through the keys like this:

student = {"name": "Ana", "age": 20, "course": "Math"}

for key in student.keys():
    print(key)

Output:

name
age
course

In most cases, this also works:

for key in student:
    print(key)

Both are valid. For beginners, student.keys() is more explicit because it clearly shows that you are looping through keys.

When to use keys() #

Use keys() when:

  • you only need the dictionary keys
  • you want to display available fields
  • you want to inspect what keys exist in a dictionary

Example:

product = {"name": "Book", "price": 12.99, "stock": 5}

print("Available fields:")
for key in product.keys():
    print(key)

Output:

Available fields:
name
price
stock

You may also see membership checks like this:

product = {"name": "Book", "price": 12.99}

print("name" in product.keys())

This works and returns True.

However, this is usually simpler:

print("name" in product)

If you want to learn that pattern, see how to check if a key exists in a dictionary in Python.

Common beginner confusion #

Here are some common mistakes beginners make with keys().

Expecting keys() to return key-value pairs #

keys() returns only the keys.

Example:

data = {"name": "Ana", "age": 20}
print(data.keys())

Output:

dict_keys(['name', 'age'])

If you want both keys and values, use items().

Expecting a list instead of dict_keys #

Many beginners expect this:

['name', 'age']

But keys() returns:

dict_keys(['name', 'age'])

If you need a real list, convert it:

data = {"name": "Ana", "age": 20}
key_list = list(data.keys())

print(key_list)

Trying to use keys() on a list or string #

keys() only works on dictionaries.

This causes an error:

numbers = [1, 2, 3]
print(numbers.keys())

You may get an AttributeError because lists do not have a keys() method. If that happens, see how to fix 'object has no attribute' errors in Python.

Forgetting parentheses #

This is a very common mistake:

data = {"name": "Ana"}
print(data.keys)

That prints the method object, not the keys.

Use:

print(data.keys())

Common mistakes #

These are the most common problems related to keys():

  • Using keys() on a non-dictionary object
  • Thinking keys() returns values too
  • Trying to index dict_keys like a normal list
  • Forgetting to convert dict_keys to a list when needed

For example, this may not work the way you expect:

data = {"a": 1, "b": 2}
keys = data.keys()

print(keys)
# print(keys[0])  # This may cause an error

If you need indexing, convert first:

data = {"a": 1, "b": 2}
keys = list(data.keys())

print(keys[0])

Debugging tips #

If something is not working, these quick checks can help:

print(type(my_dict))
print(my_dict)
print(my_dict.keys())
print(list(my_dict.keys()))

These help you confirm:

  • whether the variable is really a dictionary
  • what data it contains
  • what keys() returns
  • what the keys look like as a list

FAQ #

What does dictionary keys() return in Python? #

It returns a dict_keys view object containing all keys in the dictionary.

Is dict.keys() a list? #

No. It returns a dict_keys object. You can convert it with list(dict.keys()).

Can I loop through dict.keys()? #

Yes. You can use it in a for loop.

Do I need to use keys() to loop through a dictionary? #

No. You can loop directly over the dictionary, but keys() makes your intent clearer.

How do I get the keys as a list? #

Use list(my_dict.keys()).

See also #

Press Esc to close