I am trying to compare two dictionaries one key:value pair each. The keys in the two dictionaries are not the same. The values consist of a list of multiple numbers. I want to find all value numbers that appear in both dictionaries but my code returns the following error: unhashable type: "list". Any ideas how I can solve this error? Thanks in advance for any support!
d_MS = {"74286565":[1672118498, 72935438163394562, 3597763396, 1099812539549011970]}
d_eco = {"36146779": [170742628, 3597763396, 247113642, 1130696607027138560, 162853322]}
d1_values = set(d_MS.values())
d2_values = set(d_eco.values())
in_both = d1_values & d2_values
not_in_both = d1_values ^ d2_values
What I would like to get is a list of all numbers that appear in both dictionaries, in this example this would be
[3597763396]
Intersection of two given sets A and B is a set which consists of all the elements which are common to both A and B.
Ex.
d_MS = {"74286565":[1672118498, 72935438163394562, 3597763396, 1099812539549011970]}
d_eco = {"36146779": [170742628, 3597763396, 247113642, 1130696607027138560, 162853322]}
x = list(set(d_MS['74286565']).intersection(d_eco['36146779']))
#or
#x = list(set(*d_MS.values()).intersection(*d_eco.values()))
print(x)
O/P:
[3597763396]
Use * with it, it will unpack the values to the list, as .values()
returns a dict_values
object.
d_MS = {"74286565":[1672118498, 72935438163394562, 3597763396, 1099812539549011970]}
d_eco = {"36146779": [170742628, 3597763396, 247113642, 1130696607027138560, 162853322]}
d1_values = set(*d_MS.values())
d2_values = set(*d_eco.values())
in_both = d1_values & d2_values
not_in_both = d1_values ^ d2_values
print(in_both)
print(not_in_both)
Output:
{3597763396}
{1130696607027138560, 247113642, 1099812539549011970, 1672118498, 170742628, 72935438163394562, 162853322}
If your use case requires list, you can do this:
list(in_both)
Output
[3597763396]
Note : It will only work if there is one key:value pair each, as specified by you in your use case.
the .values()
methods returns a dict_values
object, convert it to a list which will return a list of the with the values. In this case, there is only one value which will be a list itself.
d_MS = {"74286565":[1672118498, 72935438163394562, 3597763396, 1099812539549011970]}
d_eco = {"36146779": [170742628, 3597763396, 247113642, 1130696607027138560, 162853322]}
# here the `dict_value` object is converted into a list, and the list of values
# has the one item, which was a list in the first place hence the indexing
d1_values = set(list(d_MS.values())[0])
d2_values = set(list(d_eco.values())[0])
in_both = d1_values & d2_values
not_in_both = d1_values ^ d2_values
print(in_both, not_in_both)
this will print
{3597763396} {1130696607027138560, 247113642, 1099812539549011970, 1672118498, 170742628, 72935438163394562, 162853322}
Another way to do it would be to unpack the values from the dict_values
object and hence set(*d_MS.values())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.