Introduction to Python Dictionary Keys
The dictionary.keys() associated with Python’s dictionaries displays the list of all of the keys in the form of a view object that it returns. The view object displays the changes, whensoever the dictionary is changed. Python’s dictionary.keys() does not require any arguments or parameters to be passed along with this function. The keys() function returns a view object that displays the list of all the keys associated with the dictionary.
Syntax:
The syntax of Python’s keys() function as mentioned below:
dictionary.keys()
- keys() is an attribute or function which is declared under the dictionary
- As mentioned, it does not require any arguments when called.
How Does Python Dictionary Keys Work?
Pythons in-built efficient table structure having the key’s or hash values is called dict. This dictionary’s contents are written as a series of keys and values as key:value pair is enclosed in curly braces {}. The empty dict is just an empty pair of curly braces.
Suppose we are creating a dictionary of a, o ,g having the values as alpha, beta & gamma, named dict then, in that case, the keys can be retrieved using the below statement:
dict.keys()
and the output will be:
dict_keys([‘a’, ‘o’, ‘g’])
dict_keys([])
Examples to Implement Python Dictionary Keys
Below are the examples of Python Dictionary Keys:
Example #1
Code:
## Python program to understand the usage of dictionary keys() function
per = { 'name' : 'A' , 'age' : 20 , 'salary' : 500000.00 , 'city' : 'Delhi' , 'country' : 'IND' }
print(per.keys())
emp_dict = {}
print(emp_dict.keys())
Output:
Example #2
Code:
## Python program to understand the usage of dictionary keys() function, after updating the dictionary
per = { 'name' : 'A' , 'age' : 20 , 'salary' : 500000.00 , 'city' : 'Delhi' , 'country' : 'IND' }
print('Key before update \n')
print(per.keys())
print('\nKey after update \n')
per["weight"] = 70
print(per.keys())
Output:
Here we have added one more key:value pair to the dictionary and have verified using the dict.keys() function if the view:
Declare the dictionary using the below statement.
per = { 'name' : 'A' , 'age' : 20 , 'salary' : 500000.00 , 'city' : 'Delhi' , 'country' : 'IND' }
Print the keys of that dictionary using the keys() function before updating the key:value pair.
print(per.keys())
Insert a new key:value pair in a dictionary named “per”using the below statement.
per["Weight"] = 70
Print the keys of that dictionary using the keys() function after updating the key:value pair.
print(per.keys())
Example #3
There is another way to update the dictionary using an alternate syntax and utilize a dictionary.keys() function on the same:
Code:
## Python program to understand the usage of dictionary keys() function, after updating the dictionary
per = { 'name' : 'A' , 'age' : 20 , 'salary' : 500000.00 , 'city' : 'Delhi' , 'country' : 'IND'}
print('Key before update \n')
print(per.keys())
print('\nKey after update \n')
per.update({'weight' : 70})
print(per.keys())
Output:
Example #4
Keys() can be utilized to access dictionary-like elements-like we iterate in lists and tuples. Let’s take another example to discuss the same practically, on how to use the dictionary.keys().
Code:
## Practical usage of the keys() function to iterate through dictionary object
# initializing dictionary
dict = { "EDU" : 7, "CBA" : 1, ".COM" : 2 }
# If we try to access the 2nd element using naive method
# By utilizing loops
k = 0
for j in dict:
if(k==1):
print ('The 2nd key in dict using loop : ' + j)
k = k + 1
# If we try to access the 2nd element using keys()
print ('The 2nd key in dict using keys() : ' + dict.keys()[1])
Output:
Here we have utilized two means to iterate through the dictionary:
- The very first method was the native one using loops, wherein we have used two variables j and k, to iterate through the loops
- The variable j is used to hold the dictionary object, and the same to iterate through the dictionary object by object
- where as k == 1 is utilized to fetch the 2nd key out of the dictionary, else skip the print statement.
- Whereas in the second method, we have directly utilized the keys() function to print the 2nd key out of the dictionary dict.
Conclusion
- dict.keys()is used to display the associated keys with the Python dictionary. It indeed displays all of the keys associated with that particular dictionary.
- Keys() function does not take any parameter, neither a required one nor optional.
- The contents of this dictionary are written as a series of key and values as a key : value pair enclosed in curly braces {}
- The keys() function returns a view object that displays the list of all the keys associated with the dictionary.
- No key is returned for an empty dictionary. The reason being it does not have any elements.
Recommended Articles
We hope that this EDUCBA information on “Python Dictionary Keys” was beneficial to you. You can view EDUCBA’s recommended articles for more information.