Search code examples
pythondictionary

Appending values to lists in a python dictionary


I have a dictionary to which I want to append to each drug, a list of numbers. Like this:

append(0), append(1234), append(123), etc.

def make_drug_dictionary(data):
    drug_dictionary={'MORPHINE':[],
                     'OXYCODONE':[],
                     'OXYMORPHONE':[],
                     'METHADONE':[],
                     'BUPRENORPHINE':[],
                     'HYDROMORPHONE':[],
                     'CODEINE':[],
                     'HYDROCODONE':[]}
    prev = None
    for row in data:
        if prev is None or prev==row[11]:
            drug_dictionary.append[row[11][]
    return drug_dictionary

I later want to be able to access the entirr set of entries in, for example, 'MORPHINE'.

  1. How do I append a number into the drug_dictionary?
  2. How do I later traverse through each entry?

Solution

  • Just use append:

    list1 = [1, 2, 3, 4, 5]
    list2 = [123, 234, 456]
    d = {'a': [], 'b': []}
    d['a'].append(list1)
    d['a'].append(list2)
    print d['a']