Obtain list of values of a Dictionary in Python
In this tutorial, we will learn how to obtain list of values of a Dictionary in Python
To obtain list of values of a Dictionary in Python, the dictionary offers values()
method. It returns a collection of type dict_values
.
d1 = {'Bob': 60, 'Alice': 43, 'Ram': 80, 'Geeta': 49, 'Shreyan': 95, 'Raj': 68}
values = d1.values()
print(values)
print(f'Type of values is: {type(values)}')
dict_values([60, 43, 80, 49, 95, 68])
Type of values is: <class 'dict_values'>
If we want to get all the values of a dictionary as a type of list
, we can do so using list comprehension as follows.
d1 = {'Bob': 60, 'Alice': 43, 'Ram': 80, 'Geeta': 49, 'Shreyan': 95, 'Raj': 68}
values =[value for value in d1.values()]
print(values)
print(f'Type of values is: {type(values)}')
The output of the preceding code will return a collection of values
of type list
as shown as follows.
[60, 43, 80, 49, 95, 68]
Type of values is: <class 'list'>