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>If I've understood the question correctly, perhaps this might work:</p>

<pre><code>data = [
    { 'k1': 'root3&gt;rv11;root1&gt;rv12', 'k2': 'v12', 'k3': 'v13'},
    { 'k1': 'v21', 'k2': 'root1&gt;rv21;root2&gt;rv22;', 'k3': 'v23'},
    { 'k1': 'v31', 'k2': 'v32', 'k3': 'root2&gt;rv32;'}
]

newkeys = set()
for item in data:
    add = {}

    for k, v in item.items():
        if '&gt;' in v:
            fields = v.strip(';').split(';')
            add.update(dict(f.split('&gt;') for f in fields))

    newkeys |= set(add.keys())

    item.update(add)

for nk in newkeys:
    for item in data:
        if nk not in item:
            item[nk] = None    

print(data)
</code></pre>

python dictionary