Skip to content
Advertisement

How to replace a value for a key in dictionaries of a list based on another dictionary?

Here’s a sample of the data:

JavaScript

I have over 16k dictionaries with several items in the list. What I want to do is replace the value of name in this dictionary from another dictionary which consists of the old name and new name.

JavaScript

How can I do this? One way that I could think of is the following:

JavaScript

This works, but is there a better and efficient way to do this?

Advertisement

Answer

You can use dict.get with with default parameter:

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement