Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract all values from a dictionary in Python?

I have a dictionary d = {1:-0.3246, 2:-0.9185, 3:-3985, ...}.

How do I extract all of the values of d into a list l?

like image 600
Naveen C. Avatar asked Aug 09 '11 20:08

Naveen C.


People also ask

How do you get all values from a Python dictionary?

In Python to get all values from a dictionary, we can use the values() method. The values() method is a built-in function in Python and returns a view object that represents a list of dictionaries that contains all the values.

How do I get a list of values in a dictionary?

To get a list of the dictionary's values, you can call the dict. values() function.

What Python method is used to get all the keys from a dictionary?

keys() method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion.


10 Answers

If you only need the dictionary keys 1, 2, and 3 use: your_dict.keys().

If you only need the dictionary values -0.3246, -0.9185, and -3985 use: your_dict.values().

If you want both keys and values use: your_dict.items() which returns a list of tuples [(key1, value1), (key2, value2), ...].

like image 126
Pierre Bourdon Avatar answered Oct 01 '22 12:10

Pierre Bourdon


Use values()

>>> d = {1:-0.3246, 2:-0.9185, 3:-3985}

>>> d.values()
<<< [-0.3246, -0.9185, -3985]
like image 41
zeekay Avatar answered Oct 01 '22 12:10

zeekay


For Python 3, you need:

list_of_dict_values = list(dict_name.values())
like image 32
Freddie Avatar answered Oct 01 '22 12:10

Freddie


If you want all of the values, use this:

dict_name_goes_here.values()

If you want all of the keys, use this:

dict_name_goes_here.keys()

IF you want all of the items (both keys and values), I would use this:

dict_name_goes_here.items()
like image 35
Tyler Crompton Avatar answered Oct 01 '22 11:10

Tyler Crompton


For nested dicts, lists of dicts, and dicts of listed dicts, ... you can use

from typing import Iterable

def get_all_values(d):
    if isinstance(d, dict):
        for v in d.values():
            yield from get_all_values(v)
    elif isinstance(d, Iterable): # or list, set, ... only
        for v in d:
            yield from get_all_values(v)
    else:
        yield d 

An example:

d = {'a': 1, 'b': {'c': 2, 'd': [3, 4]}, 'e': [{'f': 5}, {'g': set([6, 7])}]}
list(get_all_values(d)) # returns [1, 2, 3, 4, 5, 6, 7]

PS: Yes, I love yield. ;-)

like image 24
Michael Dorner Avatar answered Oct 01 '22 11:10

Michael Dorner


Call the values() method on the dict.

like image 29
David Heffernan Avatar answered Oct 01 '22 10:10

David Heffernan


If you want all of the values, use this:

dict_name_goes_here.values()
like image 23
Govind Sagar Avatar answered Oct 01 '22 12:10

Govind Sagar


Code of python file containing dictionary

dict={"Car":"Lamborghini","Mobile":"iPhone"}
print(dict)

If you want to print only values (instead of key) then you can use :

dict={"Car":"Lamborghini","Mobile":"iPhone"}
for thevalue in dict.values():
    print(thevalue)

This will print only values instead of key from dictionary

Bonus : If there is a dictionary in which values are stored in list and if you want to print values only on new line , then you can use :

dict={"Car":["Lamborghini","BMW","Mercedes"],"Mobile":["Iphone","OnePlus","Samsung"]}
nd = [value[i] for value in dict.values()
         for i in range(2)]
print(*nd,sep="\n")

Reference - Narendra Dwivedi - Extract Only Values From Dictionary

like image 23
Markrum Avatar answered Oct 01 '22 11:10

Markrum


d = <dict>
values = d.values()
like image 31
Gabriel Ross Avatar answered Oct 01 '22 11:10

Gabriel Ross


I know this question been asked years ago but its quite relevant even today.

>>> d = {1:-0.3246, 2:-0.9185, 3:-3985}
>>> l = list(d.values())
>>> l
[-0.3246, -0.9185, -3985]
like image 23
Chandragupta Borkotoky Avatar answered Oct 01 '22 11:10

Chandragupta Borkotoky