Updating Value in a list of dicts appears to not work

some_key in dictionaty data is a list. some_key contains a list of dictionaries. I would like to update the value of one of the keys in this list for each item.

Below is my solution to update the dicts.

data["some_key"] = [some_dict.update({"other_key": some_dict["other_key"].upper()}) for some_dict in data["some_key"]]

But this appears to output:

{'total': 1, 'some_key': [None]}

What am I doing wrong?


Solution 1:

dict.update() is an in-place operation, so it returns None to avoid confusion. That means you don't need to modify the list at all:

for some_dict in data["some_key"]:
    some_dict.update({"other_key": some_dict["other_key"].upper()})

And with that changed, even dict.update() is not necessary:

    some_dict["other_key"] = some_dict["other_key"].upper()