data_garden data_garden - 28 days ago 14
Python Question

Python - getting values from nested dicitonary

how do I build a

for loop
in order to print all
float
values in this nested
dictionary
, for any
user
?

plist = {'user1': {u'Fake Plastic Trees': 1.0, u'The Numbers': 1.0, u'Videotape': 1.0}}


desired output =
[1.0, 1.0, 1.0]

Answer

There's a dict.values() method that does exactly what you want.

a_dict = {'user1': {u'Fake Plastic Trees': 1.0, u'The Numbers': 1.0, u'Videotape': 1.0}}
first_key = list(a_dict.keys())[0]
values = a_dict[first_key].values()
print(list(values))

Output

[1.0, 1.0, 1.0]