check if two keys of a dictionary have equal values

3
Hello, I have a dictionary {'A': ['B', 'C'], 'B': ['A', 'C'], 'C': ['A', 'B'] }, I would like to know if there is any way to check if for example the 'A' key and the 'C' key have the same elements and what they are.

Thank you

    
asked by anonymous 07.09.2017 / 19:25

1 answer

1
d1 = {'A': ['B', 'C'], 'B': ['A', 'C'], 'C': ['A', 'B'], 'D': ['B', 'C']}

To test full equality

d1['A']==d1['D']
True

d1['A']==d1['B']
False

Or specifically for your question, looking for elements of A that are in C .

equals = []

for e in d1['A']:
    if e in d1['C']:
        equals.append(e)

print (equals)
['B']

Or more pythonically in just one line with list comprehension

equals = [equal for equal in d1['A'] if equal in d1['C']]
print (equals)
['B']
    
07.09.2017 / 20:01