Python dictionary values to list | Example code

Use the Python inbuild values() method to get values of the dictionary. The dict.values return a view of the dictionary’s values instead.

dictonary.values()

Python example dictionary values to list

Simple example code.

The first values() method create a view of the dictionary’s values then Use a list(item) with the view of dictionary values as an item to return a list.

dict1 = {"a": 1, "b": 2, "c": 3}

values = list(dict1.values())

print(values)

Output:

Python dictionary values to list

How can I get a list of values from dict?

Answer: You can use the * operator to unpack dict_values:

d = {1: "a", 2: "b"}

print(list(d.values()))

Output: [‘a’, ‘b’]

How to convert dictionary values to a list in Python

Answer: Use values and keys methods to convert dictionary values into a list.

dict1 = {"a": 1, "b": 2, "c": 3}

values = list(dict1.values())
keys = list(dict1.keys())

print(values)
print(keys)

Output:

[1, 2, 3]
[‘a’, ‘b’, ‘c’]

Do comment if you have any doubts and suggestions on this Python Dictionary List tutorial.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.