Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 return 86?

This does not seem to work:

print generic_drugs_mapping['MORPHINE'[0]]
like image 889
Alex Gordon Avatar asked Aug 13 '26 14:08

Alex Gordon


2 Answers

You have a bracket in the wrong place:

print generic_drugs_mapping['MORPHINE'][0]

Your code is indexing the string 'MORPHINE', so it's equivalent to

print generic_drugs_mapping['M']

Since 'M' is not a key in your dictionary, you won't get the results you expect.

like image 90
Greg Hewgill Avatar answered Aug 16 '26 04:08

Greg Hewgill


The list is the value stored under the key. The part that gets the value out is generic_drugs_mapping['MORPHINE'] so this has the value [86]. Try moving the index outside like this :

generic_drugs_mapping['MORPHINE'][0]
like image 31
Andrew Avatar answered Aug 16 '26 03:08

Andrew