Nested lists to nested dicts
<p>I've a list with master keys and a list of list of lists, where the first value of each enclosed list (like <code>'key_01'</code>) shall be a sub key for the corresponding values (like <code>'val_01', 'val_02'</code>). The data is shown here:</p>

<pre><code>master_keys = ["Master_01", "Master_02", "Master_03"]
data_long = [[['key_01','val_01','val_02'],['key_02','val_03','val_04'], ['key_03','val_05','val_06']],
           [['key_04','val_07','val_08'], ['key_05','val_09','val_10'], ['key_06','val_11','val_12']],
           [['key_07','val_13','val_14'], ['key_08','val_15','val_16'], ['key_09','val_17','val_18']]]
</code></pre>

<p>I would like these lists to be combined into a dictionary of dictionaries, like this:</p>

<pre><code>master_dic = {
"Master_01": {'key_01':['val_01','val_02'],'key_02': ['val_03','val_04'], 'key_03': ['val_05','val_06']}, 
"Master_02": {'key_04': ['val_07','val_08'], 'key_05': ['val_09','val_10'], 'key_06': ['val_11','val_12']}, 
"Master_03": {'key_07': ['val_13','val_14'], ['key_08': ['val_15','val_16'], 'key_09': ['val_17','val_18']}
}
</code></pre>

<p>What I've got so far is the sub dict:</p>

<pre><code>import itertools

master_dic = {}
servant_dic = {}
keys = []
values = []
for line in data_long:
    for item in line:
        keys.extend(item[:1])
        values.append(item[1:])
servant_dic = dict(itertools.izip(keys, values))
</code></pre>

<p>Which puts out a dictionary, as expected.</p>

<pre><code>servant_dic = {
'key_06': ['val_11','val_12'], 'key_04': ['val_08','val_07'], 'key_05': ['val_09','val_10'], 
'key_02': ['val_03','val_04'], 'key_03': ['val_05','val_06'], 'key_01': ['val_01','val_02']
 }
</code></pre>

<p>The problem is, that if I want to add the master_keys to this dictionary, so I get the wanted result, I'd have to do this in a certain order, which would be possible, if each line had a counter like this:</p>

<pre><code>enumerated_dic =
{
0: {'key_01':['val_01','val_02'],'key_02': ['val_03','val_04'], 'key_03': ['val_05','val_06']}, 
1: {'key_04': ['val_07','val_08'], 'key_05': ['val_09','val_10'], 'key_06': ['val_11','val_12']}, 
2: {'key_07': ['val_13','val_14'], ['key_08': ['val_15','val_16'], 'key_09': ['val_17','val_18']}
}
</code></pre>

<p>I'd love to do this with <code>enumerate()</code>, while each line of the <code>servant_dic</code> is build, but can't figure out how. Since afterwards, i could simply replace the counters 0, 1, 2 etc. with the <code>master_keys</code>. </p>

<p>Thanks for your help.</p>

<p>You can also use <code>pop</code> and a dict comprehension:</p>

<pre><code>for key, elements in zip(master_keys, data_long):
    print {key: {el.pop(0): el for el in elements}}
   ...:
{'Master_01': {'key_02': ['val_03', 'val_04'], 'key_03': ['val_05', 'val_06']}}
{'Master_02': {'key_06': ['val_11', 'val_12'], 'key_04': ['val_07', 'val_08'], 'key_05': ['val_09', 'val_10']}}
{'Master_03': {'key_07': ['val_13', 'val_14'], 'key_08': ['val_15', 'val_16'], 'key_09': ['val_17', 'val_18']}}
</code></pre>

python list dictionary enumerate