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_keys = list(d.keys())

print(list_of_dict_keys)

#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 keys of a dictionary and convert the keys to a list.

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]

Get Values of Dictionary as List in Python

If you want to get the values of a dictionary in Python as a list, then you use a similar method as described above.

Instead of using the keys() dictionary function, you can use the values() dictionary function.

values() returns the values of a dictionary.

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]

Hopefully this article has been useful for you to learn how to get the keys of a dictionary into a list using Python.

Categorized in:

Python,

Last Update: March 12, 2024