creating dict of dicts: looping
<p>I wanted to create a dictionary of dictionaries (DofD) using a loop. Inside the loop, my 'subdict' variable would get set and then added with a new name to the DofD. </p>

<p>I read from a file, split the contents into blocks based on a delimiter, "####", that separates what I want in each sub-dict. I name each entry in the DofD based on one of the fields within the block.</p>

<p>I was confused because my output DofD had properly named entries, but all the information across the subdicts was identical. All values reflected the last sub-dict's content.</p>

<pre><code>idxs=[i for i,val in enumerate(fileContents) if val=='####\r\n']
paramsDict={}
for i in range(len(idxs)):
    pos=idxs[i]
    if i &lt; len(idxs)-1: endLine=idxs[i+1]+1 # watch out for end of file
    else: endLine=len(fileContents)
    blockForSubDict=fileContents[idxs[i]+1:endLine]

    paramsDict[blockForSubDict[0].split()[0]]={'Name': blockForSubDict[0].split()[0], 'Values' : [float(x.replace('\r\n','')) for x in blockForSubDict[5:lenBlock]]}
</code></pre>

<p>I worked out an example by hand to create a dictionary of dictionaries to make sure I wasn't doing something odd with my syntax:</p>

<pre><code>d1={'Name': 'name1', 'Value': 30}

d2={'Name': 'name2', 'Value': 29}

d3={}
d3[d1['Name']]=d1
d3[d2['Name']]=d2 

d3
</code></pre>

<blockquote>
  <p>{'name1': {'Name': 'name1', 'Value': 30},
  'name2': {'Name': 'name2', 'Value': 29}}</p>
</blockquote>

<p>This worked.</p>

<p>What have I messed up in the loop?</p>
