How to Print a dictionary's Keys in Python

avatar

Borislav Hadzhiev

Last updated: Sep 12, 2022

banner

Photo from Unsplash

Print a dictionary's Keys in Python #

Use the dict.keys() method to print the dictionary's keys, e.g. print(list(dict.keys)). The dict.keys() method returns a view of the dictionary's keys. Pass the view to the list() class to convert it to a list if necessary.

main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'age': 30 } # ✅️ print dictionary keys as view print(my_dict.keys()) # 👉️ dict_keys(['id', 'name', 'age']) # ✅️ print dictionary keys as list print(list(my_dict.keys())) # 👉️ ['id', 'name', 'age'] # ✅️ access specific key in the dictionary print(list(my_dict.keys())[0]) # 👉️ id # ✅️ print dictionary values print(my_dict.values()) # 👉️ dict_values([1, 'bobbyhadz', 30]) # ✅️ print dictionary items for key, value in my_dict.items(): # id 1 # name bobbyhadz # age 30 print(key, value)

We used the dict.keys() method to print a dictionary's keys.

The dict.keys method returns a new view of the dictionary's keys.

main.py
my_dict = {'id': 1, 'name': 'BobbyHadz'} print(my_dict.keys()) # 👉️ dict_keys(['id', 'name'])

Something you'll often have to do is convert the view of keys to a list.

main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'age': 30 } keys = list(my_dict.keys()) print(keys) # 👉️ ['id', 'name', 'age'] print(keys[0]) # 👉️ id print(keys[2]) # 👉️ age
The keys are stored in insertion order, so you can access them consistently using indices.

If you need to print the dictionary's values, use the dict.values() method.

main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'age': 30 } print(my_dict.values()) # 👉️ dict_values([1, 'bobbyhadz', 30]) values = list(my_dict.values()) print(values) # 👉️ [1, 'bobbyhadz', 30] print(values[0]) # 👉️ 1 print(values[1]) # 👉️ bobbyhadz

The dict.values method returns a new view of the dictionary's values.

There is also a dict.items() method that is often used to iterate over the dictionary's items.

main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'age': 30 } # 👇️ [('id', 1), ('name', 'bobbyhadz'), ('age', 30)] print(list(my_dict.items())) for key, value in my_dict.items(): # id 1 # name bobbyhadz # age 30 print(key, value)

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

If you need to print a dictionary key by value, use the dict.keys() and dict.values() methods.
main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'age': 30 } key = list(my_dict.keys())[list(my_dict.values()).index('bobbyhadz')] print(key) # 👉️ name

We used the list.index() method to get the index of the value bobbyhadz in the dictionary and accessed the list of keys at the index.

We had to convert the view object of keys to a list because view objects are not subscriptable (don't allow index access).

We also had to convert the view object to a list to be able to use the list.index() method.

The list.index() method returns the index of the first item whose value is equal to the provided argument.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.