[Tutor] Inserting one dictionary into another

Keith Reed keith_reed at fastmail.net
Thu Jan 1 17:04:42 CET 2009


I'm having trouble assigning a dictionary as a value within another:


-------- Code Snippet Start --------

        for line in fromchild.readlines():
                itemarray = line.strip().split(":")
                parentdictkey = itemarray[0]
                print 'parentdictkey = ' + parentdictkey
                for index in range(len(headerinfo)):
                        nesteddict[headerinfo[index]] = itemarray[index]
                #print nesteddict
                parentdict[parentdictkey] = nesteddict
                nesteddict.clear()
        print
        '-------------------------------------------------------------------------\n'
        print parentdict

-------- Code Snippet End --------

-------- Output Start --------
{'24': {}, '25': {}, '26': {}, '27': {}, '20': {}, '21': {}, '22': {},
'23': {}, '28': {}, '29': {}, '1': {}, '0': {}, '3': {}, '2': {}, '5':
{}, '4': {}, '7': {}, '6': {}, '9': {}, '8': {}, '11': {}, '10': {},
'13': {}, '12': {}, '15': {}, '14': {}, '17': {}, '16': {}, '19': {},
'18': {}, '31': {}, '30': {}}
-------- Output End --------

The key looks correct below (it's a unique ID), but the dictionary
inserted below is empty.
If I uncomment the print statement above, the nesteddict dictionary
displays all the proper keys and values. So I know it's getting filled
OK, but it doesn't insert into the parent dictionary at all in the line:
parentdict[parentdictkey] = nesteddict

Any thoughts on how to properly insert the nested dictionary so that I
can refer to it?

-- 
  Keith Reed
  keith_reed at fastmail.net



More information about the Tutor mailing list