Skip to content Skip to sidebar Skip to footer

Printing Dictionary Values In Python

I have a dictionary like this: a={'*Initial*': {'H': 0.8, 'C': 0.2}, 'C': {'H': 0.4, 'C': 0.6}, 'H': {'H': 0.7, 'C': 0.3}} When I try to print the following: print {k:v[0] for (k,

Solution 1:

In your dictionary, "v" is the value of "k", but "v" is also another dictionary, so you are unable to index it by doing v[0], instead you need to give it a valid key like

print {k:v['H'] for (k,v) in a.items()}

which would print out the values 0.8, 0.4 and 0.7.

Post a Comment for "Printing Dictionary Values In Python"