Python Dictionary keys() Method

Python Dictionary Keys() Method

This is a detailed tutorial of the Python Dictionary keys() Method. Learn to fetch the list of keys contained in a given dictionary using this method.

Python Dictionary keys()

The method dict.keys() is used to return a list of keys present in a given dictionary as a view object. This view object is dict_keys.

Syntax

The following syntax will return the list of the keys in dict and we’re using a variable dictionaryKeysList to store the value so returned.

keys() Parameters

The Python Dictionary keys() method does not take any arguments.

keys() Return Value

This method returns the view object. This view object is labeled as dict_keys because it is the collection object for the dictionary keys.

Note. The dict_keys collection is dynamic. It means that even if the dictionary is changed later on after applying this method, the dict_keys collection will also continue to update accordingly. This is also illustrated in Example 2.

Examples

The first example illustrates the basic usage of the dict.keys() method to fetch the keys for a given dictionary and the second example shows that the view object so returned by this method continues to update on change in the original dictionary items.

Example 1. Fetching the list of keys in a Dictionary

Here I’ve defined a dictionary bio with 4 items. Then we’re fetching the dict_keys view object containing the collection of keys in bio using the Python Dictionary keys() method.


#A Dictionary with 4 items
bio = {
"name": "Gurmeet Singh",
"age" : 21,
"gender" : "MALE",
"country" : "India"
}


#Fetching Dictionary keys using keys() method
keysList = bio.keys()


print("List of Keys in bio:", keysList)


The collection of keys so returned by the method is being referred to the variable keysList which is printed to the console using the print statement.

Output. The above code prints the following code to the console on execution.

Example 2. dict_keys Object Collection gets updated if the Dictionary is Updated

In this example, we’re updating the dictionary after applying the method by adding a new item into the dictionary.


#A Dictionary with 4 items
bio = {
"name": "Gurmeet Singh",
"age" : 21,
"gender" : "MALE",
"country" : "India"
}


#Fetching Dictionary keys using keys() method
keysList = bio.keys()


print("List of Keys in bio:", keysList)


#Updating the Dictionary
#By Adding a New Item
bio["website"] = "wtmatter.com"


print("Updated List of Keys in bio:", keysList)


You can clearly see in the output that the change in the dictionary is also reflected implicitly into the list of keys. Therefore, whenever you update the original dictionary, the change is also reflected in the dict_keys view object collection.

Output.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *