To get the values of a dictionary in a list in Python, you can use the dictionary values() function and convert it to a list with list().
d = {"a":3, "b": 5, "c":1, "d":2}
list_of_dict_values = list(d.values())
print(list_of_dict_values)
#Output:
[3, 5, 1, 2]
When working with dictionaries, the ability to get information about the keys and values easily can be valuable.
One such case is if you want to get the values of a dictionary and convert the values to a list.
To get the values of a dictionary as a list in Python, you can use the dictionary values() function and convert it to a list with list().
Below shows you how to get dictionary values into a list in Python.
d = {"a":3, "b": 5, "c":1, "d":2}
list_of_dict_values = list(d.values())
print(list_of_dict_values)
#Output:
[3, 5, 1, 2]
Get Keys of Dictionary as List in Python
If you want to get the keys of a dictionary in Python as a list, then you use a similar method as described above.
Instead of using the values() dictionary function, you can use the keys() dictionary function.
keys() returns the keys of a dictionary.
To get the keys of a dictionary as a list in Python, you can use the dictionary keys() function and convert it to a list with list().
Below shows you how to get dictionary keys into a list in Python.
d = {"a":3, "b": 5, "c":1, "d":2}
list_of_dict_keys = list(d.keys())
print(list_of_dict_keys)
#Output:
[3, 5, 1, 2]
Hopefully this article has been useful for you to learn how to get the values of a dictionary into a list using Python.