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 will face the problem with the key ordering in a dictionary. The keys (or the key-value pairs) in the dictionary are sorted arbitrarily. The order is not fixed and can change in different steps.</p>

<p>To mitigate this problem you can use the <code>OrderedDict</code> from the module <code>collections</code>.</p>

<p>If I understand your problem, you want to bring in line your dictionaries and make them have the same keys. If there is a key in <code>doc1</code>, but <code>doc2</code> is missing it, so <code>doc2</code> should be updated with this key and the value 0. If that's right, then you rather want to duplicate the keys, not the values.</p>

<p>This is my try:</p>

<pre><code>from collections import OrderedDict

# your initial data
my_dict = {
    '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
    }
}

# get all keys in a help list
list_of_keys = []
for key in my_dict:
    for dockey in my_dict[key]:
        if dockey not in list_of_keys:
            list_of_keys.append(dockey)
# sort the list
list_of_keys.sort()
# the list looks like this
# ['individu', 'logistik', 'manajemen', 'tahu', 'transaksi', 'transfer']

# now we can iterate through our data
# and align the keys and values in an OrderedDict

my_ordered_dict = OrderedDict()
# get the doc keys and update the ordered dictionary
for key in my_dict:
    my_ordered_dict.update([(key, OrderedDict())])

for i in list_of_keys:
    for key in my_dict:
        if i in my_dict[key].iterkeys():
            my_ordered_dict[key].update([(i, 1)])
        else:
            my_ordered_dict[key].update([(i, 0)])
</code></pre>

<p>Now you have all keys in every dictionary, they are sorted and the order is preserved. The initial data stays unchanged.</p>

<p>I didn't use any advanced features like list comprehensions, ternary operators or similar. The approach is a little bit naive and simple because you are a beginner, and I think it would be easier for you to understand every step.</p>

<p>I hope it can help you.</p>

python dictionary