Create dictionary with join in python
<p>Each row of my document contains a dictionary of informations :</p>

<pre><code>{'O':34, 'D': 75, '2015-01':{'c':30,'f':90},'2016-01':{'c':100,'f':78,'r':90.5}}
</code></pre>

<p>The expected is :</p>

<pre><code>{'2015-01,c':30,'2015-01,f':90,'2016-01,c':100....}
</code></pre>

<p>I tried this in python :</p>

<pre><code>for row in cursor:
       d = dict((''.join([l,',',row[l].keys()]),row[l].values())
                     for l in row.keys - ['O','D'])
</code></pre>

<p>But I got this error :
TypeError: sequence item 2: expected str instance, dict_keys found
Any help please.
Thank you</p>

<p>Assuming your nesting is always one level deep, you could do following:</p>

<pre><code>d = {'O':34, 'D': 75, '2015':{'c':30,'f':90},'2016':{'c':100,'f':78,'r':90.5}}

def join_dict(d):
    for k, v in d.items():
        if isinstance(v, dict):
            for k2, v2 in v.items():
                yield '{},{}'.format(k, k2), v2


result = {k: v for k, v in join_dict(d)}
print(result)
# {'2016,f': 78, '2016,r': 90.5, '2016,c': 100, '2015,f': 90, '2015,c': 30}
</code></pre>

python dictionary join