83
Python Dictionary keys() Method
Python keys() method is used to fetch all the keys from the dictionary. It returns a list of keys and an empty list if the dictionary is empty. This method does not take any parameter. Syntax of the method is given below.
Signature
Parameters
No parameter
Return
It returns a list of keys. None if the dictionary is empty.
Let’s see some examples of keys() method to understand it’s functionality.
Python Dictionary keys() Method Example 1
Let’s first see a simple example to fetch keys from the dictionary.
Output:
dict_keys(['name', 'brand', 'price'])
Python Dictionary keys() Method Example 2
Output:
product price is too high
Python Dictionary keys() Method Example 3
We can use this method into our python program. Here, we are using it into a program to check our inventory status.
Output:
We have sufficient inventory for the bananas
Next TopicPython Dictionary