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?
You can merge two dictionaries by iterating over the key-value pairs of the second dictionary with the first one.
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.
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.
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.
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.
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.
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.
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}
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}
Can also:
updated = {**default, **parsed}
Outputs:
{'val1': 60, 'val2': 50, 'val3': 30, 'val4': 40}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With