Transform values of a dictionary to new dictionary
<p>I have a dictionary:</p>

<pre><code>{
    'doc0': {
        'individu': 1,
        'manajemen': 1,
        'tahu': 1,
        'logistik': 1,
        'transaksi': 1
    },
    'doc1': {
        'manajemen': 1,
        'transfer': 1,
        'individu':1,
        'tahu':1,
        'transaksi': 1,
        'logistik': 1
    },
    'doc2': {
        'manajemen': 1,
        'logistik': 1,
        'transaksi': 1
    }
}
</code></pre>

<p>I want use Python to transform it to the following in a new dictionary:</p>

<pre><code>{
    'doc0': {
        'individu': 1,
        'manajemen': 1,
        'tahu': 1,
        'logistik': 1,
        'transaksi': 1,
        'transfer':0
    },
    'doc1': {
        'individu': 1,
        'manajemen': 1,
        'tahu': 1,
        'logistik': 1,
        'transaksi': 1
    },
    'doc2': {
        'individu': 0,
        'manajemen': 1,
        'tahu': 0,
        'logistik': 1,
        'transaksi': 1,
        'transfer':0
    }
}
</code></pre>

<p>You can take a look at the following code :</p>

<pre><code>&gt;&gt;&gt; all = list(set([j for i in list(d.keys()) for j in list(d[i].keys())]))

&gt;&gt;&gt; all
['transfer', 'tahu', 'transaksi', 'individu', 'manajemen', 'logistik']

&gt;&gt;&gt; for k in all:
     for j in list(d.keys()):
         if not k in d[j].keys():
             d[j][k]=0

&gt;&gt;&gt; d
{'doc0': {'individu': 1,
  'logistik': 1,
  'manajemen': 1,
  'tahu': 1,
  'transaksi': 1,
  'transfer': 0},
 'doc1': {'individu': 1,
  'logistik': 1,
  'manajemen': 1,
  'tahu': 1,
  'transaksi': 1,
  'transfer': 1},
 'doc2': {'individu': 0,
  'logistik': 1,
  'manajemen': 1,
  'tahu': 0,
  'transaksi': 1,
  'transfer': 0}}
</code></pre>

<p>I take all keys in the nested dictionary, and then create the values that don't exist (hence the zeroes in the last output).</p>

python dictionary