Python keys function is used to return the list of available total keys in a dictionary. In this section, we discuss how to use this Dictionary keys function, and the syntax behind this is:
dictionary_name.keys()
Python Dictionary keys function Example
It returns the list of keys available in a given one. The below code prints the keys in emp and empl.
TIP: Please refer to the Dictionary to understand them in Python.
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 program, we are going to insert a new value into the dict and print keys. Next, we updated a value and displayed the keys.
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'])