# Get items in one dictionnary but not the other one - Python
Data Science for Political and Social Phenomena
Author: Guillaume Redoulès

# Get items in one dictionnary but not the other one

Say we have two similar dictonnaries, we want to find all the items that are in the second dictonnary but not in the first one.

    dict1 = {"Banana": "yellow", "Orange": "orange"}
    dict2 = {"Banana": "yellow", "Orange": "orange", "Lemon":"yellow"}

In the following code we find the difference of the keys and then rebuild a dict taking the corresponding values.

    difference = { k : dict2[k] for k in set(dict2) - set(dict1) }
    difference

    {'Lemon': 'yellow'}

Be careful, this operation is not symetric. This means that if we want to find a value present in dict1 but not in dict2 this code won't work

    dict1 = {"Banana": "yellow", "Orange": "orange", "Lemon":"yellow"}
    dict2 = {"Banana": "yellow", "Orange": "orange"}
    difference = { k : dict2[k] for k in set(dict2) - set(dict1) }
    difference

    {}

### Find an error or bug? Have a suggestion?

Everything on this site is avaliable on GitHub. Head on over and submit an issue. You can also message me directly by email.