Python keys Dictionary function is used to return the list of available keys in a dictionary (total keys). In this section, we discuss how to use this Dictionary keys function. The syntax behind this Dictionary keys function is:
dictionary_name.keys()
Python Dictionary keys Example
The dictionary key function returns the list of keys available in a given dictionary. The below dictionary code print the keys in amp, and empl.
TIP: Please refer to the Dictionary to understand Python Dictionaries.
# Python Dictionary keys Example emp = {'name': 'Kevin', 'age': 25 , 'job': 'HR', 'Sal': 725000} print("Dictionary: ", emp) # Print Keys print("\nDictionary Keys: ", emp.keys()) # Creating an Empty Dictionary empl = {} print("Dictionary: ", empl) # Print Keys print("\nDictionary Keys: ", empl.keys())
keys in a Dictionary Example 2
In this program, we are going to insert a new value into the dictionary and print keys. Next, we updated a value and displayed the dictionary keys.
# Python Dictionary keys Example emp = {'name': 'Kevin', 'age': 25 , 'job': 'HR'} print("Dictionary: ", emp) # Print Keys print("\nDictionary Keys: ", emp.keys()) # Print Keys emp['age'] = 27 print("\nDictionary Keys: ", emp.keys()) # Print Keys emp['Sal'] = 725000 print("\nDictionary Keys: ", emp.keys())