Converting Dictionary Values to List in Python
You can get all the values of a dictionary as a list. To do this, you need to apply the list
function. Let's see in practice. Let's say we have the following dictionary:
dct = {
'a': 1,
'b': 2,
'c': 3
}
Let's get all its values as a list:
res = list(dct)
print(res) # ['a', 'b', 'c']
Given a dictionary:
dct = {
1: 'ab',
2: 'cd',
3: 'ef'
}
Get the following list from it:
[1, 2, 3]
Given a dictionary:
dct = {
'x': 1,
'y': 2,
'z': 3
}
Get the following list from it:
['z', 'y', 'x']