0

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'}]
1
  • I think by dictionary['job'][i], you mean that you need to find the index of the current dictionary in the list. Check out the enumerate() function to help with this. Alternatively, you can use the zip() function to combine your two lists into a single list that you can iterate over. Then you won't need the index. Commented Nov 23, 2022 at 21:30

3 Answers 3

1

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'}]
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Good one. the second method same as me but you are iterating over new jobs list instead.
1

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'}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.