Home »
Python
Python Dictionary values() Method with Example
Python Dictionary values() Method: Here, we are going to learn how to get the values of the dictionary as a view object?
Submitted by IncludeHelp, on November 26, 2019
Dictionary values() Method
values() method is used to get all values of a dictionary, it returns a view object that contains the all values of the dictionary as a list.
Syntax:
dictionary_name.values()
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is <class 'dict_values'>, it returns all values as a view object that contains a list of all values.
Example:
# Python Dictionary values() Method with Example
# dictionary declaration
student = {
"roll_no": 101,
"name": "Shivang",
"course": "B.Tech",
"perc" : 98.5
}
# printing dictionary
print("data of student dictionary...")
print(student)
# getting all values
x = student.values()
print(x)
# printing type of values() Method
print('Type is: ',type(student.values()))
# changing the value
# it will effect the value of view object also
student['course'] = 'MCA'
# printing dictionary
print("data of student dictionary...")
print(student)
# getting all values
x = student.values()
print(x)
Output
data of student dictionary...
{'roll_no': 101, 'name': 'Shivang', 'course': 'B.Tech', 'perc': 98.5}
dict_values([101, 'Shivang', 'B.Tech', 98.5])
Type is: <class 'dict_values'>
data of student dictionary...
{'roll_no': 101, 'name': 'Shivang', 'course': 'MCA', 'perc': 98.5}
dict_values([101, 'Shivang', 'MCA', 98.5])
TOP Interview Coding Problems/Challenges