How to print a dictionary's key?

To print the keys of a dictionary in Python, you can use the built-in keys() method. Here is an example:

my_dict = {'a': 1, 'b': 2, 'c': 3}

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

This will output:

a
b
c

Watch a course Python - The Practical Guide

Another way to do this is to use the items() method, which returns a list of key-value pairs, and then extract the keys using a list comprehension:

my_dict = {'a': 1, 'b': 2, 'c': 3}

print([key for key, value in my_dict.items()])

This will output:

['a', 'b', 'c']

If you are using python3.7 and above you can also use the dict.keys() as an object which can be used directly in print statement,

my_dict = {'a': 1, 'b': 2, 'c': 3}

print(my_dict.keys())

This will output:

dict_keys(['a', 'b', 'c'])