Home > Net >  How to replace values in nested dictionaries by new values in a list in python
How to replace values in nested dictionaries by new values in a list in python

Time:11-24

I am iterating through a list of dictionaries. I need to update the values for one specific key in all the dictionaries and I have the new values stored in a list. The list of new values is ordered so that the 1st new value belongs to a key in the 1st dictionary, 2nd new value to a key in the 2nd dictionary, etc.

My data looks something like this:

dict_list = [{'person':'Tom', 'job':'student'},
            {'person':'John', 'job':'teacher'},
            {'person':'Mary', 'job':'manager'}]
new_jobs = ['lecturer', 'cook', 'driver']

And I want to transform the list of dictionaries using the list of new jobs according to my description into this:

dict_list = [{'person':'Tom', 'job':'lecturer'},
            {'person':'John', 'job':'cook'},
            {'person':'Mary', 'job':'driver'}]

As I have a very long list of dictionaries I would like to define a function that would do this automatically but I am struggling how to do it with for loops and zip(), any suggestions?

I tried the for loop below. I guess the following code could work if it was possible to index the dictionaries like this dictionary['job'][i] Unfortunately dictionaries don't work like this as far as I know.

def update_dic_list():
    for dictionary in dict_list:
        for i in range(len(new_jobs)):
            dictionary['job'] = new_jobs[i]
        print(dict_list)

The output the code above gave me was this:

[{'person': 'Tom', 'job': 'driver'}, {'person': 'John', 'job': 'teacher'}, {'person': 'Mary', 'job': 'manager'}]
[{'person': 'Tom', 'job': 'driver'}, {'person': 'John', 'job': 'driver'}, {'person': 'Mary', 'job': 'manager'}]
[{'person': 'Tom', 'job': 'driver'}, {'person': 'John', 'job': 'driver'}, {'person': 'Mary', 'job': 'driver'}]

CodePudding user response:

If your new_jobs list has the right job for each corresponding entry in the dict list, you could use zip:

dict_list = [{'person':'Tom', 'job':'student'},
            {'person':'John', 'job':'teacher'},
            {'person':'Mary', 'job':'manager'}]
new_jobs = ['lecturer', 'cook', 'driver']

for d, j in zip(dict_list, new_jobs):
    d['job'] = j

print(dict_list)

prints

[{'person': 'Tom', 'job': 'lecturer'}, {'person': 'John', 'job': 'cook'}, {'person': 'Mary', 'job': 'driver'}]

CodePudding user response:

With your loop, for each dictionary, you're going through the new jobs and updating that same dictionary over and over with each of the jobs until the last one. So by the end of it, they'll all be drivers. Because that's the last one.

You can do

dict_list = [{'person':'Tom', 'job':'student'},
            {'person':'John', 'job':'teacher'},
            {'person':'Mary', 'job':'manager'}]
new_jobs = ['lecturer', 'cook', 'driver']


def update_dic_list():
    for job, _dict in zip(new_jobs, dict_list):
        _dict['job'] = job

or

def update_dict_list():
    for i, job in enumerate(new_jobs):
        dict_list[i]['job'] = job

CodePudding user response:

You only need to remove the inner loop because you are changing dictionary key job more than one time for each of item of outer loop:

def update_dic_list():
    i = 0
    for dictionary in dict_list:
            dictionary['job'] = new_jobs[i]
            i  = 1
    print(dict_list)

Or alternatively you could use enumerate:

def update_dic_list():
    for i, dictionary in enumerate(dict_list):
            dictionary['job'] = new_jobs[i]
    print(dict_list)

Output:

[{'person': 'Tom', 'job': 'lecturer'}, {'person': 'John', 'job': 'cook'}, {'person': 'Mary', 'job': 'driver'}]
  • Related