I’m trying to compare two dictionaries. My approach is to turn them into two separate lists of tuples and to then use the set module. Here is an illustration:
dict = {'red':[1,2,3],'blue':[2,3,4],'green':[3,4,5]}
dict1 = {'green':[3,4,5],'yellow':[2,3,4],'red':[5,2,6]}
intersection = set(set(dict.items()) & set(dict1.items()))
apparently, this is comparing two lists of tuples and python doesn’t like that. I get a TypeError: ‘list’ is unhashable error (or similar wording).
I would like intersection to contain [('green',[3,4,5])]. Any ideas?
You can even make this into a function:
Obviously, if you prefer to not have the output in dictionary form, you can just remove the
dict()call and replace with with list comprehension brackets ([]) instead.