Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get list of values from dict?

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.

like image 461
Muhd Avatar asked Apr 26 '13 03:04

Muhd


People also ask

Can we convert dictionary to list in Python?

Python's dictionary class has three methods for this purpose. The methods items(), keys() and values() return view objects comprising of tuple of key-value pairs, keys only and values only respectively. The in-built list method converts these view objects in list objects.

How do you print a dictionary list in Python?

So we have to get the dictionaries present in the list according to the key. We can get this by using dict. items().

How can I get a list of all keys in a dictionary?

To get a list of all keys from a dictionary, you can simply use the dict. keys() function.

How do I get a list of keys and values in Python?

The methods dict. keys() and dict. values() return lists of the keys or values explicitly. There's also an items() which returns a list of (key, value) tuples, which is the most efficient way to examine all the key value data in the dictionary.


1 Answers

dict.values returns a view of the dictionary's values, so you have to wrap it in list:

list(d.values()) 
like image 178
jamylak Avatar answered Oct 07 '22 15:10

jamylak