C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python Dictionary values() MethodPython values() method is used to collect all the values from a dictionary. It does not take any parameter and returns a dictionary of values. It returns an empty dictionary if the dictionary has no value. The syntax and examples of this method are given below. Signaturevalues() ParametersNo Parameter ReturnIt returns a dictionary of values. Let?s see some examples of values() method to understand it?s functionality. Python Dictionary values() Method Example 1It is a simple example which returns all the values from the dictionary. An example is given below. # Python dictionary values() Method # Creating a dictionary einventory = {'Fan': 200, 'Bulb':150, 'Led':1000} # Calling function stock = einventory.values() # Displaying result print("Stock available",einventory) Output: Stock available {'Fan': 200, 'Bulb': 150, 'Led': 1000} Python Dictionary values() Method Example 2If the dictionary is already empty, this method also returns an empty dictionary except any error or exception. See an example below. # Python dictionary values() Method # Creating a dictionary einventory = {} # Calling function stock = einventory.values() # Displaying result print("Stock available",einventory) Output: Stock available {} Python Dictionary values() Method Example 3This example uses various methods to get information about dictionary such as length, keys etc. # Python dictionary values() Method # Creating a dictionary einventory = {'Fan': 200, 'Bulb':150, 'Led':1000} # Applying functions length = len(einventory) print("Total number of values:",length) keys = einventory.keys() print("All the Keys:",keys) item = einventory.items() print("Items:",einventory) p = einventory.popitem() print("Deleted items:",p) stock = einventory.values() print("Stock available",einventory) Output: Total number of values: 3 All the Keys: dict_keys(['Fan', 'Bulb', 'Led']) Items: {'Fan': 200, 'Bulb': 150, 'Led': 1000} Deleted items: ('Led', 1000) Stock available {'Fan': 200, 'Bulb': 150}
Next TopicPython Dictionary
|