3
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]]
wxs
  • 288
  • 3
  • 18
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

2 Answers2

6

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Not sure what you meant with the second part but it is wrong 'M' is not a key in the dict that he setup. – Andrew Jul 30 '10 at 22:29
  • 1
    and if `'M'` was a key in his dictionary, he still wouldn't get the expected results, unless 'M' mapped to `86` ;-) – John Machin Jul 30 '10 at 22:35
2

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]
Andrew
  • 2,943
  • 18
  • 23