I have two lists
list1 = ['a', 'b', 'c', 'd']
list2 = ['e', 'f', 'g', 'h']
I know from before that some of these elements are related through another list
ref_list = [
['d', 'f'], ['a', 'e'], ['b', 'g'], ['c', 'f'], ['a', 'g'],
['a', 'f'], ['b', 'e'], ['b', 'f'], ['c', 'e'], ['c', 'g']
]
I would like to quickly identify the two groups from list1
and list2
which have all the possible pairs [list1 element, list2 element]
in ref_list
.
In this case the solution would be
[['a', 'b', 'c'], ['e', 'f', 'g']]
I can think of some ways to do this for such small lists but need help if list1
, list2
and ref_list
have thousands of elements each.