0

lets say I have the following dictionary

dict1= {"a":[1,2,3],"b":[2,3,4]}

how would I make it so the output is:

[2,3]

where the values match in a dictionary.

Dylan
  • 39
  • 6
  • 1
    Hi! Welcome to StackOverflow. Could you walk us through what you have tried so far and why it doesn't work? – Quelklef Sep 14 '22 at 03:46
  • 2
    Are you asking [how to compute the intersection of multiple lists?](https://stackoverflow.com/questions/3852780/python-intersection-of-multiple-lists) Or are you having trouble accessing the lists in the dictionary at all? – Grismar Sep 14 '22 at 03:48

1 Answers1

2

you can refer to my code bellow:

arr1 = d['a']
arr2 = d['b']
arrResult = []
for i in arr1:
  if i in arr2:
    arrResult.append(i)
print(arrResult)

and here is the result: [2, 3]

More advanced you can use:

x = set.intersection(*map(set, d.values()))    
print(x)
hungtv
  • 94
  • 4
  • Great! What would happen if we had infinite number of keys and tried to find the intersection of the values in the keys? – Dylan Sep 14 '22 at 04:01
  • I have added, you can refer on my reply. if found helpful, rate my answer to help the next person. Thanks! – hungtv Sep 14 '22 at 04:07