Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two dictionaries, remove key/value pair in one dict if it exists in the other

I have two dictionaries. One looks like this:

dict1 = {'ana': 'http://ted.com', 'louise': 'http://reddit.com', 'sarah':'http://time.com'}

The other one looks like this:

dict2 = {'patricia': 'http://yahoo.com', 'ana': 'http://ted.com',
         'louise': 'http://reddit.com', 'florence': 'http://white.com'}

I need to compare the two dictionaries, and eliminate from dict2 any key/value pair already present in dict1

As you can see, Ana and Louise already exist in dict1, so I'd like to automatically delete it from dict2 The output expected would contain only elements unique to dict2 and not already present in dict1, and would look like:

dict2 = {'patricia': 'http://yahoo.com', 'florence': 'http://white.com'}

I don't need to do anything about Sarah being in dict1. I only care about comparing dict2 with dict1 to remove duplicates.

Extra info:

I tried to loop over the dicts in many different ways but it gave me two types of errors: not hashable type or dict content changed during action.

I also tried to make each into a list and combine the lists, but the end result is another list and I don't know how to turn a list back into a dictionary.

like image 767
skeitel Avatar asked Feb 17 '17 00:02

skeitel


People also ask

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.

How to get the common key-value pairs from two dictionaries in Python?

We can get the common key-value pairs from two dictionaries and check if those are equal or not. We will be using Python For loop to the common key-value pairs. Create two dictionaries that contain some properties. Create a blank dictionary that will contain the common key-value pairs.

How do you delete a key from dict1 and dict2 together?

This simply looks for all the keys in dict1 that are in dict2 and then deletes the key/value pairs from dict2. for key in dict1: if key in dict2 and (dict1 [key] == dict2 [key]): del dict2 [key]

Why can't I compare dictionaries with lists?

Therefore you are not comparing dictionaries. If you try and use a list as a dictionary key your code will not run. You have no objects for which to compare. This is like typing x = dict (23\;dfg&^*$^%$^$%^) then complaining how the comparison does not work with the dictionary. Of course it will not work.


1 Answers

Jim's answer removes items if the keys match. I think you wanted to remove if both key and value matched. This is actually very easy since you're using Python 3:

>>> dict(dict2.items() - dict1.items())
{'florence': 'http://white.com', 'patricia': 'http://yahoo.com'}

It works because dict_items objects treat subtraction operations as set differences.

like image 176
wim Avatar answered Sep 30 '22 08:09

wim