How can I get list of values from dict?

Muhd picture Muhd · Apr 26, 2013 · Viewed 956k times · Source

How can I get a list of the values in a dict in Python?

In Java, getting the values of a Map as a List is as easy as doing list = map.values();. I'm wondering if there is a similarly simple way in Python to get a list of values from a dict.

Answer

jamylak picture jamylak · Apr 26, 2013

Yes it's the exact same thing in Python 2:

d.values()

In Python 3 (where dict.values returns a view of the dictionary’s values instead):

list(d.values())