The values method in Python is utilized to obtain a view object that displays the values contained within a dictionary. This view object presents the dictionary's values in the form of a list. Furthermore, it is important to note that the view object automatically updates to reflect any modifications made to the dictionary.
Syntax of the values Method in Python Dictionary
Below is the syntax for the values method belonging to dictionaries:
Syntax:
dict_name.values()
Parameters:
- No Parameter
- It provides a view object that contains the values.
Examples of Dictionary values
We will now examine several instances of utilizing the values method available in dictionaries.
Example 1: Working of the values Method
This illustration demonstrates the operation of the values method in Python.
Example
# Simple Example of Python dictionary values() Method
# given dictionary
electrical_inventory = {
'Fan' : 200,
'Bulb' : 150,
'Led' : 1000,
'Socket' : 500,
'Tubelight' : 600
}
print("Electrical Inventory:", electrical_inventory)
# Calling function
stock = electrical_inventory.values()
# Displaying result
print("Stock available:", stock)
Output:
Electrical Inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'Socket': 500, 'Tubelight': 600}
Stock available: dict_values([200, 150, 1000, 500, 600])
Explanation:
In this instance, we are provided with a dictionary. We utilized the values method to obtain the view object that represents the values extracted from the dictionary.
Example 2: Returning Values from an Empty Dictionary
In the event that the dictionary is devoid of any elements, this method will still yield an empty view object without triggering any errors or exceptions. Refer to the example provided below:
Example
# Simple Example of Python dictionary values() Method
# given dictionary
sample_dict = {}
print("Empty Dictionary:", sample_dict)
# Calling function
val_obj = sample_dict.values()
# Displaying result
print("Stock available:", val_obj)
Output:
Empty Dictionary: {}
Stock available: dict_values([])
Explanation:
In this illustration, we start with an empty dictionary. We utilize the values method to obtain the view object that represents the values contained within the dictionary. Given that the dictionary is devoid of any entries, the resulting object is likewise empty.
Example 3: Reflecting Changes in the View Object
In Python, the view object mirrors any modifications made to the dictionary. Below is an illustration:
Example
# Simple Example of Python dictionary values() Method
# given dictionary
electrical_inventory = {
'Fan' : 200,
'Bulb' : 150,
'Led' : 1000,
'Socket' : 500,
'Tubelight' : 600
}
print("Electrical Inventory:", electrical_inventory)
# Calling function
stock = electrical_inventory.values()
# Displaying result
print("Stock available:", stock)
# updating items in the dictionary
electrical_inventory['Socket'] = 300
electrical_inventory['Led'] = 800
print()
print("Electrical Inventory (Updated):", electrical_inventory)
print("Stock available (Updated):", stock)
Output:
Electrical Inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'Socket': 500, 'Tubelight': 600}
Stock available: dict_values([200, 150, 1000, 500, 600])
Electrical Inventory (Updated): {'Fan': 200, 'Bulb': 150, 'Led': 800, 'Socket': 300, 'Tubelight': 600}
Stock available (Updated): dict_values([200, 150, 800, 300, 600])
Explanation:
In this illustration, a dictionary is presented to us. We employed the values method to obtain a view object that displays the values contained within the dictionary. Subsequently, we made modifications to several items within the dictionary. Consequently, the view object also mirrors the updates made to the dictionary.
Conclusion
The values function in Python serves as a robust method for dictionaries, enabling programmers to gather all the values contained within a dictionary. This method requires no parameters and produces a collection of the dictionary's values. It finds extensive application in data analytics, reporting, and data transformations across a multitude of sectors.
Python Dictionary values Method FAQs
1) What purpose does the values method of a Dictionary serve in Python?
The values method in Python is utilized to obtain a view object that contains the values extracted from a dictionary. This method does not require any arguments.
2) Are there any parameters that can be accepted by the values method?
The values method does not accept any arguments and provides a dictionary containing the values. If the dictionary is devoid of any values, it will return an empty dictionary.
3) What will the values method return when the dictionary is devoid of any entries?
In the event that the dictionary contains no elements, invoking the values method will yield an empty view object.
Let's understand this using an example:
Example
#passing an empty dictionary
dic = {}
# Applying functions
length = len(dic)
print("Total number of values:",length)
stock = dic.values()
print("Stock available",stock)
Output:
Total number of values: 0
Stock available dict_values([])
4) Is it possible to change the values of a Dictionary by utilizing the values method?
The values method of a dictionary is solely intended for retrieving all the values contained within a given dictionary. It does not execute any additional functions. Consequently, it is not possible to alter the values directly through this method. To change the values in the dictionary, you must do so directly by referencing their associated keys.