Mediterráneo
Mediterráneo

Reputation: 115

How to change multiple keys inside my dictionary?

I have a pretty complex dictionary and I would like to change the values of Name in Entity1 to Owner and Name in Entity2 in Type. I use below code but I got stuck....

KeyError: 'Name'

my_dictio = 
{'url': 'www.google.com', 'value': 
[{'Car': 'Mercedes', 'Engine': 'Hybrid', 'Entity1': {'Country': 'US', 'Name': 'Richard', 'Entity2': {'Name': 'type1', 'Payment': 'cash'}}},
{'Car': 'Audi', 'Engine': 'Hybrid', 'Entity1': {'Country': 'DE', 'Name': 'Mesut', 'Entity2': {'Name': 'type2', 'Payment': 'cash'}}}, 
{'Car': 'Volkswagen', 'Engine': 'Gas', 'Entity1': {'Country': 'FR', 'Name': 'Paul', 'Entity2': {'Name': 'type3', 'Payment': 'card'}}}]}

for i, dictionary in enumerate(my_dictionary['value']):
    my_dictionary['Origin'][i]['model'] = dictionary.pop('Name')
    
print(my_dictionary)

This is my expected output:

my_dictio = {'url': 'www.google.com', 'value': 
[{'Car': 'Mercedes', 'Engine': 'Hybrid', 'Entity1': {'Country': 'US', 'Owner': 'Richard', 'Entity2': {'Type': 'type1', 'Payment': 'cash'}}},
{'Car': 'Audi', 'Engine': 'Hybrid', 'Entity1': {'Country': 'DE', 'Owner': 'Mesut', 'Entity2': {'Type': 'type2', 'Payment': 'cash'}}}, 
{'Car': 'Volkswagen', 'Engine': 'Gas', 'Entity1': {'Country': 'FR', 'Owner': 'Paul', 'Entity2': {'Type': 'type3', 'Payment': 'card'}}}]}

Upvotes: 0

Views: 62

Answers (2)

alani
alani

Reputation: 13079

You just need to drill down through it methodically -- it may help to use other variables for the sub-dictionaries (remember that they are references, so you can modify them and you will still be modifying the relevant elements of the original dictionary):

from pprint import pprint

my_dictio = {
    'url': 'www.google.com',
    'value': [{'Car': 'Mercedes',
               'Engine': 'Hybrid',
               'Entity1': {'Country': 'US',
                           'Entity2': {'Name': 'type1',
                                       'Payment': 'cash'},
                           'Name': 'Richard'}},
              {'Car': 'Audi',
               'Engine': 'Hybrid',
               'Entity1': {'Country': 'DE',
                           'Entity2': {'Name': 'type2',
                                       'Payment': 'cash'},
                           'Name': 'Mesut'}},
              {'Car': 'Volkswagen',
               'Engine': 'Gas',
               'Entity1': {'Country': 'FR',
                           'Entity2': {'Name': 'type3',
                                       'Payment': 'card'},
                           'Name': 'Paul'}}]}

pprint(my_dictio)

for d in my_dictio['value']:

    d1 = d['Entity1']
    d1['Owner'] = d1['Name']
    del d1['Name']

    d2 = d1['Entity2']
    d2['Type'] = d2['Name']
    del d2['Name']
    
pprint(my_dictio)

Upvotes: 1

Stefan
Stefan

Reputation: 1934

What about:

for d in my_dictionary['value']:
    d['Entity1']['Owner'] = d['Entity1']['Name']
    del d['Entity1']['Name']
    d['Entity1']['Entity2']['Type'] = d['Entity1']['Entity2']['Name']
    del d['Entity1']['Entity2']['Name']

Upvotes: 1

Related Questions