Python: print values ​​from a dictionary

generic_drugs_mapping={'MORPHINE':[86],
                       'OXYCODONE':[87],
                       'OXYMORPHONE':[99],
                       'METHADONE':[82],
                       'BUPRENORPHINE':[28],
                       'HYDROMORPHONE':[54],
                       'CODEINE':[37],
                       'HYDROCODONE':[55]}

How do i get it back 86?

This does not work:

print generic_drugs_mapping['MORPHINE'[0]]
+3
source share
2 answers

A list is a value stored under a key. The part that receives the value is equal generic_drugs_mapping['MORPHINE'], so the value matters [86]. Try moving the index like this:

generic_drugs_mapping['MORPHINE'][0]
+2
source

You have a bracket in the wrong place:

print generic_drugs_mapping['MORPHINE'][0]

Your code indexes the string 'MORPHINE', so it is equivalent

print generic_drugs_mapping['M']

Since it is 'M'not the key in your dictionary, you will not get the expected results.

+6
source

Source: https://habr.com/ru/post/1757421/


All Articles