Comparing content of one dict to anoter

Viewed 38

i want to take the d1 dict and the d2 dict and check if the items in the d2 dict lists exists in the d1 dict no matter in what order.

The way i imagine i would do this is. Take the first list from d2, take the individual items and check if they exist in d1["X"]. I want to return a boolean if all three items in the d2 list is found in d1["x"].

How should i approach this? Thanks in advance for all help.

  1 
  2 d1 = {"x":["A1","A2","A3","B2","C1"], "o":[]}
  3 
  4 d2 = {1:["A1","B1","C1"], 2:["A2","B2","C2"], 3:["A3","B3","C3"],
  5       4:["A1","A2","A3"], 5:["B1","B2","B3"], 6:["C1","C2","C3"],
  6       7:["A1","B2","C3"], 8:["A3","B2","C1"]}
  7 
  8 
  9 for x in d2.values():
 10     if x in d1["x"]:
 11         print(x)

2 Answers

You can try comprehension with all()

For each value list in second dictionary, check if all the item in this value list is there in the first dictionary value list for key x

[all(each in d1['x'] for each in valueList) for valueList in d2.values()]

#output: [False, False, False, True, False, False, False, True]

This seems an set theory problem, python set are the right way.

chk = set(d1['x'])
[set(v).issubset(chk) for v in  d2.values()]

#out
[False, False, False, True, False, False, False, True]
Related