Python Dictionary keys() MethodPython 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. SignatureParametersNo parameter ReturnIt 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 1Let's first see a simple example to fetch keys from the dictionary. Output: dict_keys(['name', 'brand', 'price']) Python Dictionary keys() Method Example 2Output: product price is too high Python Dictionary keys() Method Example 3We 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
|