You are currently viewing Python Dictionary keys() Method Usage

Python dictionary keys() method is used to get the list of all the keys in the dictionary. Dictionary in Python is a collection of key-value pairs and is also known as “mappings” because they “map” or “associate” key objects to value objects. The keys() method returns a view object which contains a list of all keys related to the Python dictionary.

Advertisements

In this article, I will explain the syntax and usage of the python dictionary keys() method with examples

All dictionary methods are available on the dictionary methods page..

Quick Examples of Python Dictionary keys() Method

Following are quick examples of how to use the Dictionary keys() method.



# Example 1: Using keys() to get keys as a list
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
print(technology.keys())

# Example 2: Use keys() method on empty dictionary
technology=dict()
print(technology.keys())

# Example 3: Update the keys() method if update the dictionary
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
print(technology.keys())
technology.update({"tutor":"Richard"})
print(technology.keys())

# Example 4: Verify the key in the dictionary
technology={
            "Course":"python",
            "Fee":4000,
            "Duration":"60 days"
            }
if "Duration" in technology.keys():
    print("Duration key is present in the dictionary")
else:
    print("Duration key is not present in the dictionary")

1. Syntax of the keys()

Following is the syntax of the Python Dictionary keys() method.


# Syntax of the keys()
dict.keys()

1.2 parameters of the keys() in Python Dictionary

The keys() method doesn’t allow any parameters

1.3 Return value of the keys()

It returns a view object which contains a list of all keys related to the Python dictionary. If the dictionary allows any changes which will reflect in the view object.

2. Usage of keys() method in python dictionary

In Dictionary keys() are unique meaning no duplicate keys are allowed, and each key has its corresponding value, so it doesn’t make sense to map a particular key more than once. If you specify a key a second time during the initial creation of a dictionary, then the second occurrence will update the first.

In Python, a dictionary key must be an immutable type such as a number(int, float), string, and tuple. However, neither a list nor another dictionary can act as a dictionary key, because lists and dictionaries are mutable. On the other hand, values can allow any type of object in dictionaries.

2.1 Dictionary keys() Example

When we use keys() method we get all keys present in the dictionary as a view object, which contains a list of all keys.


# Using keys() to get keys as a list
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
print(technology.keys())

# Outputs: 
dict_keys(['Course', 'Fee', 'Duration'])

# Use keys() method to empty dictionary
technology=dict()
print(technology.keys())

# Output:
dict_keys([])

From the above, using the keys() method we have got a view object which contained a list of all keys present in the dictionary.

2.2 Return a View Object After Modification of Dictionary

Any changes in the python dictionary will reflect in the view object. The dictionary can be updated by using the update() method. For example,


# Update the keys() method if update the dictionary
technology={"Course":"python","Fee":4000,"Duration":"60 days"}
print(technology.keys())

# Output:
dict_keys(['Course', 'Fee', 'Duration'])

technology.update({"tutor": "Richard"})
print(technology.keys())
# Output: 
dict_keys(['Course', 'Fee', 'Duration', 'tutor'])

Note that keys() method has been updated when the dictionary is updated.

3. Verify If the key is Present in the Python dictionary

A common usage of the key () method is to verify if the key is present in the dictionary. We can check whether the key is in the dictionary or not by using the Python if...in statement. This statement is often used with the combination of an if...in statement. For instance,


# Verify the key in the dictionary
technology={
            "Course":"python",
            "Fee":4000,
            "Duration":"60 days"
            }
if "Duration" in technology.keys():
    print("Duration key is present in the dictionary")
else:
    print("Duration key is not present in the dictionary")

# Output:
Duration key is present in the dictionary

4. Conclusion

In this article, I have explained about usage Python dictionary keys() method, how to access the key elements present in the dictionary with examples. I have also explained how to update the keys() method after modifying the dictionary. And also explained how to verify the key in a dictionary by using Python if...in a statement.

Happy Learning !!

References