Getting All Values from a Dictionary in Python
In Python, you can also get all the values from a dictionary. To do this, use the values
method. Nothing is passed to its parameter. The method returns a special object dict_values
.
Let us have the following dictionary:
dct = {
'a': 1,
'b': 2,
'c': 3
}
Let's derive all the values from it:
res = dct.values()
print(res) # dict_values([1, 2, 3])
To make it easier to work with the dict_values
object, you can convert it to a list. This is done using the list
function:
res = list(dct.values())
print(res) # [1, 2, 3]
Given a dictionary:
dct = {
'x': 1,
'y': 2,
'z': 3
}
Get all its values.
Given a dictionary:
dct = {
1: 'x',
2: 'y',
3: 'z',
4: 'w'
}
Get all its values.
Given a dictionary:
dct = {
'x': 1,
'y': 2,
'z': 3
}
Get a list of meanings for this dictionary.
The following dictionaries are given:
dct1 = {
'a': 1,
'b': 2,
'c': 3
}
dct2 = {
1: 'a',
2: 'b',
3: 'c'
}
Get their values in the following form:
[1, 2, 3, 'a', 'b', 'c']