Skip to content

Python dictionary values to list | Example code

  • by

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 the list

Simple example code.

The first values() method creates 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.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading