Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare dicts and merge them. No overwrite and no duplicate values

I made a mistake in my question here (wrong requested input and expected output): Comparing dicts, updating NOT overwriting values

I am not looking for this solution: Combining 2 dictionaries with common key So this question is not a duplicate

Problem statement:

requested input:

d1 = {'a': ['a'], 'b': ['b', 'c']}
d2 = {'b': ['c', 'd'], 'c': ['e','f']}

expected output (I don't care about the order of the keys / values!):

new_dict = {'a': ['a'], 'b': ['b', 'c', 'd'], 'c': ['e', 'f']}

The solution in Combining 2 dictionaries with common key gives following output:

new_dict = {'a': ['a'], 'b': ['b', 'c', 'c', 'd'], 'c': ['e', 'f']}

I don't want the duplicates to be stored.

My solution (it works but it is not so efficient):

unique_vals = []
new_dict = {}
for key in list(d1.keys())+list(d2.keys()) :
    unique_vals = []
    try:
        for val in d1[key]:
            try:
                for val1 in d2[key]:
                    if(val1 == val) and (val1 not in unique_vals):
                        unique_vals.append(val)
            except:
                continue
    except:        
        new_dict[key] = unique_vals
    new_dict[key] = unique_vals


for key in d1.keys():
    for val in d1[key]:
        if val not in new_dict[key]:
            new_dict[key].append(val)
for key in d2.keys():
    for val in d2[key]:
        if val not in new_dict[key]:
            new_dict[key].append(val)
like image 272
aze45sq6d Avatar asked Oct 29 '19 11:10

aze45sq6d


People also ask

How do I compare and merge two versions of a document?

Compare and merge two versions of a document You can compare two versions of a document to see how they differ by viewing revision marks. You can also merge two versions of the same document into one new document.

How to unpack and merge two dictionaries in Python?

Items in Python can be unpacked using either the * or the ** characters. For dictionaries, to access both the key and value, you need to use the ** characters. Let’s see how we can use this to merge two dictionaries in Python:

How to merge two dicts without empty values in Python?

to do this we can just create a dict without the empty values and then merge them together this way: d1 = {'a':1, 'b':1, 'c': '', 'd': ''} d2 = {'a':2, 'c':2, 'd': ''} merged_non_zero = { k: (d1.get (k) or d2.get (k)) for k in set (d1) | set (d2) } print (merged_non_zero)

How to compare two dictionaries in Python and find similarities?

SO let’s start learning how to compare two dictionaries in Python and find similarities between them. Basically A dictionary is a mapping between a set of keys and values. The keys support the basic operations like unions, intersections, and differences. When we call the items () method on a dictionary then it simply returns the (key, value) pair.


2 Answers

Here is how I would go about it:

d1 = {'a': ['a'], 'b': ['b', 'c']}
d2 = {'b': ['c', 'd'], 'c': ['e','f']}
dd1 = {**d1, **d2}
dd2 = {**d2, **d1}
{k:list(set(dd1[k]).union(set(dd2[k]))) for k in dd1}

Produces the desired result.

like image 93
quest Avatar answered Oct 31 '22 05:10

quest


I suggest using a default dictionary collection with a set as a default value. It guarantees that all values will be unique and makes the code cleaner.

Talking about efficiecy it's O(n^2) by time.

   from collections import defaultdict


   d1 = {'a': ['a'], 'b': ['b', 'c']}
   d2 = {'b': ['c', 'd'], 'c': ['e','f']}

   new_dict = defaultdict(set)

   for k, v in d1.items():
       new_dict[k] = new_dict[k].union(set(v))

   for k, v in d2.items():
       new_dict[k] = new_dict[k].union(set(v))

like image 26
Max Voitko Avatar answered Oct 31 '22 07:10

Max Voitko