Increase the number of columns in a list of dictionaries
<p>I don't know the exact words to phrase this in a more scientific way, so please feel free to help me out with my poor grammar.</p>

<p>My problem is the following: I have constructed a table out of a list of dictionaries like so:</p>

<pre><code>for d in listDictionary:
    print d

{'key1':'value11', 'key2':'value12', ..., 'keyM':'value1M'}
{'key1':'value21', 'key2':'value22', ..., 'keyM':'value2M'}
...
{'key1':'valueN1', 'key2':'valueN2', ..., 'keyM':'valueNM'}
</code></pre>

<p>For simplicity, you can picture this as an <code>N*M</code> matrix. Like so:</p>

<pre><code>key1       key2      ...   keyM
value11    value12   ...   value1M
value21    value22   ...   value2M
...
valueN1    valueN2   ...   valueNM
</code></pre>

<p><strong>Now</strong>, here's the real problem. Some of the values need further processing but it's not always clear how much more. For example, 
let's pick the <code>Ith</code> value from keyK. <code>0&lt;K&lt;M, 0&lt;I&lt;N</code> therefore we have <code>valueIK</code>.</p>

<p><code>valueIK</code> will be either <code>n/a</code> or represent a tree with <code>siblings</code> and <code>children</code>.</p>

<p>The tree representation is something like this:</p>

<pre><code>root1
    -&gt; somevalue1

root2
    -&gt; somevalue2

root3
    -&gt; somevalue3
</code></pre>

<p>So in plain text, <code>valueIK</code> will be: <code>root1&gt;somevalue1;root2&gt;somevalue2;root3&gt;somevalue3;</code>
I want to be able to expand the matrix/listDictionary because valueIK has more items than the initial assumption. But in order to maintain a constant size in the dictionary the other entries in the list have to be updated as well.</p>

<p>ex. If we have this as our initial matrix</p>

<pre><code>key1       key2      ...  keyK   ...   keyM
value11    value12   ...         ...   value1M
value21    value22   ...         ...   value2M
...                       valueIK
valueN1    valueN2   ...         ...   valueNM
</code></pre>

<p>I want to accomplish something like this:</p>

<pre><code>key1       key2      ...  keyK      root1           root2         ...   keyM
value11    value12   ...            n/a             whatevervalue ...   value1M
value21    value22   ...            someothervalue  n/a           ...   value2M
...                       valueIK   somevalue1      n/a
valueN1    valueN2   ...            n/a             helloWorld    ...   valueNM
</code></pre>

<p>In other words, the rest of the entries <strong>might</strong> have some values under the key <code>root_</code> and we <strong>don't</strong> want to alter them but if they have nothing we have to fill the matrix cell with <code>'n/a'</code></p>

<p>Sorry for the very long and technical post. I tried to be as analytical as I could be.
I can't figure out a way to do this on my own, that's why I'm asking for help. </p>

<p>Thanks :)</p>

<p>Firstly, the simple answer:</p>

<p>If you want to set only unset values, there's a <code>dict.setdefault</code> method. For example, given <code>dict5</code> as the fifth row (containing <code>value51</code>, <code>value52</code>, etc.), and that you want to update the column <code>root1</code>:</p>

<pre><code># returns the value of dict5['root1'], setting it to 'n/a' if it didn't exist at all
dict5.setdefault('root1', 'n/a')
</code></pre>

<p>However, a note: Since you're building your table out of dictionaries for each row, you shouldn't actually need to set unused things to <code>'n/a'</code> in most cases - you can safely leave elements of one dictionary unset and set things in another, because none of the dictionaries are actually checking each other. This will save memory, and you won't need to spend time setting all the empties. Just make sure that when you're getting things out, you use <code>get</code> instead of <code>[]</code>, e.g.:</p>

<pre><code># Returns dict5['root1'], but returns 'n/a' if that doesn't exist.
dict5.get('root1', 'n/a')
</code></pre>

<p>If you do that, though, you will have to keep a separate list of all the columns somewhere and loop through that, if you were previously looping through each dict somehow, e.g.:</p>

<pre><code># Prints each cell of the table on a new line, including 'n/a' for empty cells
columns = ['key1', 'key2', 'key3']
for d in listDictionary:
    for c in columns:
        print d.get(c, 'n/a')

# Instead of:
for d in listDictionary:
    for c in d:  # Only gets the keys that exist in that particular dict, so no 'n/a's
        print d.get[c]
</code></pre>

python dictionary