The Python dictionary keys function is used to return a view object that contains the list of available or total keys in a dictionary. In this section, we discuss how to use this dictionary’s key function, and the syntax behind this is:
dictionary_name.keys()
The return object (dict_keys) is dynamic, meaning that if there are any changes to the original dictionary (updating or deleting items) automatically reflect in the dict.keys() view object.
Python Dictionary keys function Example
As we mentioned earlier, the dict.keys() method returns the list of keys available in a given dictionary. The code below prints the keys in the emp dictionary. Next, we used the dict.keys() function on an empty dictionary, and without raising any error, it returns an empty dict_keys view object.
TIP: Please refer to the Python Dictionary article to understand the creation and its functionality in the Python programming language. For more, refer to the dictionary methods article.
emp = {'name': 'Kevin', 'age': 25 , 'job': 'HR', 'Sal': 725000}
print("Dictionary: ", emp)
# Print
print("\nDictionary Keys: ", emp.keys())
# Creating an Empty Dictionary
empl = {}
print("Dictionary: ", empl)
# Print
print("\nDictionary Keys: ", empl.keys())

In this dictionary program, we are going to insert a new value into the dict and print keys. Next, we updated a value and displayed the keys. To get all dictionary values, use the values() method and for key-value pair combination, use the items() function. However, to access a particular value of a given key, use the get() function.
emp = {'name': 'Kevin', 'age': 25 , 'job': 'HR'}
print(emp)
print(emp.keys())
emp['age'] = 27
print(emp.keys())
emp['Sal'] = 725000
print(emp.keys())
{'name': 'Kevin', 'age': 25, 'job': 'HR'}
dict_keys(['name', 'age', 'job'])
dict_keys(['name', 'age', 'job'])
dict_keys(['name', 'age', 'job', 'Sal'])
Python dictionary keys to list
In the older versions, we can use the built-in key() function to extract and return the dictionary key as a dict_keys object. If we use the list() function, we can convert the return dict_key object to a list of dictionary keys.
emp = {'name': 'Kevin', 'age': 25, 'Sal': 725000}
n = list(emp.keys())
print(n)
['name', 'age', 'Sal']
If you are using Python 3.7 or later versions, you don’t have to use the key() method. Instead, directly use the list() function on a dictionary to get the list of dict keys.
emp = {'name': 'Kevin', 'age': 25, 'Sal': 725000}
n = list(emp)
print(n)
['name', 'age', 'Sal']
What is the difference between dict.keys() and looping through dict?
There is no difference between Python dictionary keys() function and iterating over the dict because both of them return the keys in a dictionary. When it comes to performance, there is a marginal edge to iterating the dictionary as it is slightly faster compared to the dict.keys().
The following example uses the for loop with and without keys to iterate over the dictionary and to show that they are the same.
emp = {'name': 'Tracy', 'age': 25 , 'Sal': 750000}
for key in emp:
print(key, end = ' ')
print('\n------')
for key in emp.keys():
print(key, end = ' ')
name age Sal
------
name age Sal