Python Dictionary values()
is used to get a list of values stored in a dictionary. Elements in the dictionary are in the form of key-value pairs and each key has its corresponding value
. A value can be any data type such as a number(int, float), a string, a list, a tuple, or even another dictionary. In fact, you can use a value of any valid type in Python.
In this article, I will explain the syntax and usage of the python Dictionary values method with examples
All dictionary methods are available on the dictionary methods page..
Quick Examples of The Python Dictionary values()
Following are quick examples of how to use the Dictionary values() method.
# Example 1: Using Values() to get values from the dictionary
courses = { 1:'java', 2:'python', 3:'pandas',4:'sparks'}
print(courses.values())
# Example 2: Modifying view object after changing dictionary
courses = { 1:'java', 2:'python', 3:'pandas',4:'sparks'}
print("course:",courses.values())
courses.update({5:'c',6:'c++'})
print("new curses:",courses.values())
# Example 3: Using for loop to iterate values
courses = { 1:'java', 2:'python', 3:'pandas',4:'sparks'}
for item in courses.values():
print("values:", item)
1. Syntax of the values()
Following is the syntax of the Python Dictionary values() method.
# Syntax of the values
dict.values()
1.2 Parameters of the values()
It doesn’t allow any parameters.
1.3 Return Value of the Values()
It returns a view object that contains a list of all the values stored in the dictionary. Any changes or updates in the dictionary will reflect in the view object.
2. Usage of Python Dictionary values() Method
Python dictionary values() method returns a view object that contains a list of all the values stored in the dictionary. This method is useful if you want to iterate over only the values in the dictionary. Any changes in the dictionary will reflect in the view object.
If you want to access the values stored in a dictionary without calling keys, values() method comes in handy. Elements in the dictionary are in the form of key-value pairs and each key has its corresponding value
. A value can be any data type such as a number(int, float), a string, a list, a tuple, or even another dictionary. In fact, you can use a value of any valid type in Python.
2.1 Get All Values From the Python Dictionary
Let’s create a dictionary, and use values() method to get all values from the dictionary.
# Using Values() to get values from the dictionary
courses = { 1:'java', 2:'python', 3:'pandas',4:'sparks'}
print(courses.values())
# Outputs:
dict_values(['java', 'python', 'pandas', 'sparks'])
Here we have created a Python dictionary named courses
that having courses data, then using courses.values()
, we have got a list of all the values
stored in the dictionary.
2.2 Get Values After Modifying The Dictionary
When we make any change in the dictionary, it is also reflected in the view object. For instance,
# Modifying view object after changing dictionary
courses = { 1:'java', 2:'python', 3:'pandas',4:'sparks'}
print("course:",courses.values())
courses.update({5:'c',6:'c++'})
print("new curses:",courses.values())
# Outputs:
course: dict_values(['java', 'python', 'pandas', 'sparks'])
new curses: dict_values(['java', 'python', 'pandas', 'sparks', 'c', 'c++'])
Here, I have taken a dictionary named Courses
and updated it using the update() method. Notice that the values have been updated in the view object.
3. Iterate Values Python Dictionary
On the other hand, we can also use the dictionary values()
method in Python for loop
and get each value of the dictionary. For example,
# Using for loop to iterate values
courses = { 1:'java', 2:'python', 3:'pandas',4:'sparks'}
for item in courses.values():
print("values:", item)
# Outputs:
values: java
values: python
values: pandas
values: sparks
The result is the same as the view object of values() method, the only difference is values have formed separate lines.
4. Conclusion
In this article, I have explained the Dictionary values() method. By using values() method we access all values of the dictionary as a view object. In addition, I have also explained changing the dictionary will also reflect on the view onject.
Happy Learning !!
Related Articles
- get() Method of Python Dictionary
- pop() Method of Python Dictionary
- clear() Method of Python Dictionary
- items() Method of Python Dictionary
- fromkeys() Method of Python Dictionary
- copy() Method of Python Dictionary
- keys() Method of Python Dictionary
- setdefault() Method of Python Dictionary
- popitem() Method of Python Dictionary
- update() Method of Python Dictionary