0

I have a dictionary like this:

{'some_company_100': {'key1': 'value1',
  'key2': 'value2',
  'key3': 'value3',
  'key4': 'value4',
  'key5': 'value5',
  'key6': {'key6_1': 'value6_1', 'key6_2': 'value6_2'},
  'key7': 'value7'},
 'some_company_101': {'key1': 'valuea',
  'key2': 'valueb',
  'key3': 'valuec',
  'key4': 'valued',
  'key5': 'valuee',
  'key6': {'keyf_1': 'valuef_1', 'keyf_2': 'valuef_2'},
  'key7': 'value7'}}

What I need to take out these inner dictionary out to get rid of the nested dictionary. The way I've tried:

for key, value in final_dict.iteritems():
    for k, v in value.copy().iteritems():
        if isinstance(v, dict):
            value.update(v)
            del[k]

But the problem is that the iteration in the inner dictionary must be doing on copy() of the dictionary so I can't delete k if v isinstance on my original dictionary after getting out v. If I exclude .copy() from value.copy().iteritems(). it returns me an error: `RuntimeError: dictionary changed size during iteration. And to mention, I tried to create this to be universal, not to use

if k=='key6':
    do something

.

Desired output:

{'some_company_100': {'key1': 'value1',
  'key2': 'value2',
  'key3': 'value3',
  'key4': 'value4',
  'key5': 'value5',
  'key6_1': 'value6_1',
  'key6_2': 'value6_2',
  'key7': 'value7'},
 'some_company_101': {'key1': 'valuea',
  'key1': 'valueb',
  'key2': 'valuec',
  'key4': 'valued',
  'key5': 'valuee', 
  'key6_1': 'value6_a', 
  'key6_2': 'value6_b'
  'key7': 'value7'}}
jovicbg
  • 1,523
  • 8
  • 34
  • 74

2 Answers2

0

You can just create a new dictionary, and use a temporary dictionary to add new values to. This will allow the original dictionary to be unchanged.

Example:

result = {}
for inner in d:
    temp = {}
    for key in d[inner]:
        curr = d[inner][key]
        if isinstance(curr, dict):
            temp.update(curr)
        else:
            temp[key] = curr

    result[inner] = temp

print(result)

Which Outputs:

{'some_company_100': {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4', 'key5': 'value5', 'key6_1': 'value6_1', 'key6_2': 'value6_2', 'key7': 'value7'}, 'some_company_101': {'key1': 'valuea', 'key2': 'valueb', 'key3': 'valuec', 'key4': 'valued', 'key5': 'valuee', 'keyf_1': 'valuef_1', 'keyf_2': 'valuef_2', 'key7': 'value7'}}
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
0

Here, though I don't think it's quite optimal, this code works as excpected ! :D

import copy

dictionnary = {'some_company_100': {'key1': 'value1',
                      'key2': 'value2',
                      'key3': 'value3',
                      'key4': 'value4',
                      'key5': 'value5',
                      'key6': {'key6_1': 'value6_1', 'key6_2': 'value6_2'},
                      'key7': 'value7'},
 'some_company_101': {'key1': 'valuea',
                      'key2': 'valueb',
                      'key3': 'valuec',
                      'key4': 'valued',
                      'key5': 'valuee',
                      'key6': {'keyf_1': 'valuef_1', 'keyf_2': 'valuef_2'},
                      'key7': 'value7'}}
keys_to_delete = []
keys_to_add = []
for company in dictionnary:
    for key in dictionnary[company]:
        if type(dictionnary[company][key]) == type({}):
            for nested_key in dictionnary[company][key]:
                keys_to_add.append((company, copy.copy(nested_key), copy.copy(dictionnary[company][key][nested_key])))
            keys_to_delete.append((company, key))

for key in keys_to_delete:
    del dictionnary[key[0]][key[1]]


for key in keys_to_add:
    dictionnary[key[0]][key[1]] = key[2]
print(dictionnary)
Saauan
  • 1
  • 2