Print all keys in a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Sep 16, 2022

banner

Photo from Unsplash

Print all keys in a dictionary in Python #

To print all keys in a dictionary:

  1. Use a for loop to iterate over the dictionary.
  2. Use the print() function to print each key.
  3. The variable in the for loop stores the key of the current iteration.
main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } for key, value in my_dict.items(): # name bobbyhadz # age 30 # website bobbyhadz.com print(key, value) for key in my_dict: # name # age # website print(key) keys = list(my_dict.keys()) print(keys) # 👉️ ['name', 'age', 'website'] values = list(my_dict.values()) print(values) # 👉️ ['bobbyhadz', 30, 'bobbyhadz.com'] items = list(my_dict.items()) # 👇️ [('name', 'bobbyhadz'), ('age', 30), ('website', 'bobbyhadz.com')] print(items)

We used a for loop to print all keys in a dictionary.

main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } for key in my_dict: # name # age # website print(key)

The variable in the for loop gets assigned the key of the current iteration.

If you need to print all key-value pairs in the dictionary, use the dict.items() method.
main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } for key, value in my_dict.items(): # name bobbyhadz # age 30 # website bobbyhadz.com print(key, value)

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

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

Use a formatted string literal if you need to format the keys in any way when printing them.

main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } for key, value in my_dict.items(): # name=bobbyhadz # age=30 # website=bobbyhadz.com print(f'{key}={value}')
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
var1 = 'bobby' var2 = 'hadz' result = f'Name: {var1}{var2}' print(result) # 👉️ Name: bobbyhadz

Make sure to wrap expressions in curly braces - {expression}.

If you need to get a list of the dictionary's keys, use the dict.keys() method.

main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } keys = list(my_dict.keys()) print(keys) # 👉️ ['name', 'age', 'website'] values = list(my_dict.values()) print(values) # 👉️ ['bobbyhadz', 30, 'bobbyhadz.com']

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

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

Make sure to use the list() class if you need to convert the view objects to a list.

If you need to join the dictionary's keys into a string, use the str.join() method.

main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } keys = ', '.join(my_dict.keys()) print(keys) # 👉️ name, age, website values = ', '.join(str(value) for value in my_dict.values()) print(values) # 👉️ bobbyhadz, 30, bobbyhadz.com items = ', '.join(f'{key}: {value}' for key, value in my_dict.items()) print(items) # 👉️ name: bobbyhadz, age: 30, website: bobbyhadz.com

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We used a comma and a space as the separator, but you can use any other delimiter.

Here is an example that uses a newline (\n) character as the delimiter.

main.py
my_dict = { 'name': 'bobbyhadz', 'age': 30, 'website': 'bobbyhadz.com' } keys = '\n'.join(my_dict.keys()) # name # age # website print(keys) items = '\n'.join(f'{key}: {value}' for key, value in my_dict.items()) # name: bobbyhadz # age: 30 # website: bobbyhadz.com print(items)

When the keys in the dictionary are joined with a newline (\n) character separator, they are printed on separate lines.

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.