Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two dictionaries with preference to one of them - [duplicate]

I have two dictionaries

One:

default = {"val1": 10, "val2": 20, "val3": 30, "val4": 40}

Two:

parsed = {"val1": 60, "val2": 50}

Now, I want to combine these two dictionaries in such a way that values for the keys present in both the dictionaries are taken from the parsed dictionary and for rest of the keys in default and their values are put in to the new dictionary.

For the above given dictionary the newly created dictionary would be,

updated = {"val1": 60, "val2": 50, "val3": 30, "val4": 40}

The obvious way to code up this would be to loop through the keys in default and check if that is present in parsed and put then into a new list updated, and in the else clause of the same check we can use values from default.

I am not sure if this is a pythonic way to do it or a much cleaner method. Could someone help on this?

like image 435
Rohan Asokan Avatar asked Jan 02 '21 12:01

Rohan Asokan


People also ask

Can you merge two dictionaries?

You can merge two dictionaries by iterating over the key-value pairs of the second dictionary with the first one.

How do I merge two dictionaries in a single expression?

Using | in Python 3.9 In the latest update of python now we can use “|” operator to merge two dictionaries. It is a very convenient method to merge dictionaries.

How do you combine two dictionary values for common keys?

Using Counter The Counter function from the Collections module can be directly applied to merge the two dictionaries which preserves the keys. And in turn adds the values at the matching keys.

How do you combine two dictionaries in Python?

Python combine dicts To combine dicts in Python, use the dict.update () method. The dict.update () is a built-in Python method that updates the dictionary with the items from another dictionary. If we have two dictionaries and we want to merge them, then we will call the update () method on dictionary 2 and pass dictionary 1 as an argument.

What do you learn when merging Python dictionaries?

You’ll also learn how to append list values when merging dictionaries. Python dictionaries are incredibly important data structures to learn. They are often used for their speedy data retrieval. Python dictionaries share many attributes with JSON format, which is often used in storing web data.

How to initialize two dictionaries in Python?

First of all, initialize two dictionary variables with few items included. dict_first and dict_second are two dictionaries we have initialized. The first dictionary has five pairs and the second dictionary also has five key-value pairs.

Is it possible to use a non-default comparer for input dictionaries?

A minor detail: along with most other answers, this doesn't deal with the situation where (some of) the input dictionaries are using a non-default comparer: e.g. a case-insensitive string key. A completely general solution should allow the caller to specify the comparer for the target dictionary.


3 Answers

You can create a new dict with {**dict1,**dict2} where dict2 is the one that should have priority in terms of values

>>> updated = {**default, **parsed}
>>> updated
{'val1': 60, 'val2': 50, 'val3': 30, 'val4': 40}
like image 63
abc Avatar answered Oct 29 '22 03:10

abc


First, create a copy of the dict with values you want to keep using the dict.copy() method Then, use the dict.update() method to update the other dict into the create dict copy:

default = {"val1": 10, "val2": 20, "val3": 30, "val4": 40}
parsed = {"val1": 60, "val2": 50}

updated = default.copy()
updated.update(parsed)

print(updated)

Output:

{'val1': 60, 'val2': 50, 'val3': 30, 'val4': 40}
like image 35
Ann Zen Avatar answered Oct 29 '22 03:10

Ann Zen


Can also:

updated = {**default, **parsed}

Outputs:

{'val1': 60, 'val2': 50, 'val3': 30, 'val4': 40}
like image 39
sheldonzy Avatar answered Oct 29 '22 04:10

sheldonzy