Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key : value pair.
keys() method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary.
Syntax: dict.keys()
Parameters: There are no parameters.
Returns: A view object is returned that displays all the keys. This view object changes according to the changes in the dictionary.
Example #1:
# Python program to show working # of keys in Dictionary # Dictionary with three keys Dictionary1 = {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} # Printing keys of dictionary print(Dictionary1.keys()) # Creating empty Dictionary empty_Dict1 = {} # Printing keys of Empty Dictionary print(empty_Dict1.keys()) |
Output:
dict_keys(['A', 'B', 'C']) dict_keys([])
Order of these key values in the list may not always be same.
Example #2: To show how updation of dictionary works
# Python program to show updation # of keys in Dictionary # Dictionary with two keys Dictionary1 = {'A': 'Geeks', 'B': 'For'} # Printing keys of dictionary print("Keys before Dictionary Updation:") keys = Dictionary1.keys() print(keys) # adding an element to the dictionary Dictionary1.update({'C':'Geeks'}) print('\nAfter dictionary is updated:') print(keys) |
Output:
Keys before Dictionary Updation: dict_keys(['B', 'A']) After dictionary is updated: dict_keys(['B', 'A', 'C'])
Here, when the dictionary is updated, keys is also automatically updated to show the changes.
Practical Application : The keys() can be used to access the elements of dictionary as we can do for list, without use of keys(), no other mechanism provides means to access dictionary keys as list by index. This is demonstrated in the example below.
Example #3 : Demonstrating practical application of keys()
# Python program to demonstrate # working of keys() # initializing dictionary test_dict = { "geeks" : 7, "for" : 1, "geeks" : 2 } # accessing 2nd element using naive method # using loop j = 0for i in test_dict: if (j == 1): print ('2nd key using loop : ' + i) j = j + 1 # accessing 2nd element using keys() print ('2nd key using keys() : ' + test_dict.keys()[1]) |
Note: -> the second approach would not work because dict_keys in Python 3 does not support indexing.
Output :
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Different ways of sorting Dictionary by Keys and Reverse sorting by keys
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Extract selective keys' values Including Nested Keys
- Python | Split dictionary keys and values into separate lists
- Python dictionary with keys having multiple inputs
- Python | Add new keys to a dictionary
- Python program to Swap Keys and Values in Dictionary
- Python | Remove spaces from dictionary keys
- Python | Check if given multiple keys exist in a dictionary
- Python | Get dictionary keys as a list
- Python | Initialize a dictionary with only keys from a list
- Python | Ways to change keys in dictionary
- Python | Find keys with duplicate values in dictionary
- Python | Combine two dictionary adding values for common keys
- Python | Test if dictionary contains unique keys and values
- Python | Extract specific keys from dictionary
- Python | Remove Keys from dictionary starting with K
- Python | Minimum value keys in Dictionary
- Python | Sort dictionary keys to list
- Python | Add keys to nested dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

