python create nested directory based on two lists
<p>Good morning,</p>

<p>This might be easy but IÂ´m just starting with python. For learning:
Is there are 'cleaner' way creating a nested dict based on two lists than this:</p>

<pre><code>person = ['mama.a','mama.b', 'mama.c',
         'mama.d', 'papa.a', 'papa.b']
kind = ['a', 'b', 'c', 'd']
combined = {}

# GOAL:
# {'mama': {'a': [], 'c': [], 'b': [], 'd': []}, 'papa': {'a': [], 'c': [], 'b': [], 'd': []}}

for human in [i.split('.')[0] for i in person]:
    combined[human] = {}
    for attrib in kind:
        combined[human][attrib] = []
</code></pre>

<p>What about making a <code>set</code> of the names. To ensure you only try to add each one to <code>dict</code> once.</p>

<pre><code>combined = {}
for n in set(p.split('.')[0] for p in person):
    combined[n] = {k:[] for k in kind}
</code></pre>

<p>This could probably be made into a monster one-liner nested <code>dict</code> comprehension. Although I dont think it would be as easy to read.</p>

<p>or you could split the <code>set</code> out onto another line. It then becomes</p>

<pre><code>pset = set(p.split('.')[0] for p in person)
pdict = {p: {k: [] for k in kind} for p in pset}
</code></pre>

<p>Where the second line is the same as @Faiz Haldes answer.</p>

python list