Python Print Dictionary Keys Without “dict_keys”

Problem Formulation and Solution Overview

If you print dictionary keys using print(dict.keys()), Python returns a dict_keys object, i.e., a view of the dictionary keys. The representation prints the keys enclosed in a weird dict_keys(...) wrapper text, e.g., dict_keys([1, 2, 3]).

Here’s an example:

my_dict = {'name': 'Carl',
           'age': 42,
           'income': 100000}
print(my_dict.keys())
# dict_keys(['name', 'age', 'income'])

There are multiple ways to change the string representation of the keys, so that the print() output doesn’t yield the strange dict_keys view object.

Method 1: Convert to List

An easy way to obtain a pretty output when printing the dictionary keys without dict_keys(...) representation is to convert the dict_keys object to a list using the list() built-in function. For instance, print(list(my_dict.keys())) prints the dictionary keys as a simple list.

Here’s an example:

my_dict = {'name': 'Carl',
           'age': 42,
           'income': 100000}
print(list(my_dict.keys()))
# ['name', 'age', 'income']

So far, so simple. Read on to learn or recap some important Python features and improve your skills. There are many paths to Rome! πŸ›οΈ

Method 2: Unpacking

An easy and Pythonic way to print a dictionary without the dict_keys prefix is to unpack all keys into the print() function using the asterisk operator. This works because the print() function allows an arbitrary number of arguments as input. It prints those keys separated by a single whitespace character per default.

Here’s an example:

my_dict = {'name': 'Carl',
           'age': 42,
           'income': 100000}
print(*my_dict.keys())
# name age income

It cannot get any more concise, frankly. πŸ™‚

Of course, you can change the separator and end arguments accordingly to obtain more control of the output:

my_dict = {'name': 'Carl',
           'age': 42,
           'income': 100000}
print(*my_dict.keys(), sep=' x ', end=' The End')

Output:

name x age x income The End

Do you need even greater flexibility than this? No problem! See here: ‡️

Method 3: String Join Function and Generator Expression

To convert the dictionary keys to a single string object without 'dict_keys' in it and with maximal control, you can use the string.join() function in combination with a generator expression and the built-in str() function.

Here’s an example:

my_dict = {'name': 'Carl',
           'age': 42,
           'income': 100000}
print(', '.join(str(x) for x in my_dict.keys()))
# name, age, income

πŸ’‘ Note: You can replace the comma ',' with your desired separator character and modify the representation of each individual element by modifying the expression str(x) of the generator expression to something arbitrary complicated.

See here for something crazy that wouldn’t make any sense:

my_dict = {'name': 'Carl',
           'age': 42,
           'income': 100000}
print(' | '.join('x' + str(x) + 'x' for x in my_dict.keys()))
# xnamex | xagex | xincomex

Note that you could also use the repr() function instead of the str() function in this example—it wouldn’t matter too much.

Finally, I’d recommend you check out this tutorial to learn more how generator expressions work—many Python beginners struggle with this concept even though it’s ubiquitous in expert coders’ code bases. πŸ‘‡

πŸ‘‰ Recommended Tutorial: Understanding One-Line Generators in Python

If you want to print the values without the dict_values string representation, feel free to check out our full guide on the Finxter blog.


And subscribe for free cheat sheets and learning material: