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 key 'A' and the key 'C' have equal elements and what they are.
Thank you
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 key 'A' and the key 'C' have equal elements and what they are.
Thank you
0
d1 = {'A': ['B', 'C'], 'B': ['A', 'C'], 'C': ['A', 'B'], 'D': ['B', 'C']}
To test total equality
d1['A']==d1['D']
True
d1['A']==d1['B']
False
Or specifically for your question, looking for elements of A
who 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']
Browser other questions tagged python dictionary
You are not signed in. Login or sign up in order to post.
Sorry guy, I just read over your answer, I’ll remove mine.
– user62320