I have a dictionary with values as type of list, I wrote the code as follows to find common features in all lists. I dont know how to find the common features two by two, comparing only two lists ? so if we have 3 in a dictionary, how to compare first and second values and then first and third and lastly second and third? and store the result in a dictionary and generate the proper name for it as the key.
dic = {"df1":[1,2,3,4,5,555],"df2":[2,3,4,5,6,6,77,8,9,555],"df3":[1,2,2,3,4,5,6,76,7,555,5,4,3,2]}
from functools import reduce
dataframe_list = list(dic.values())
common = reduce(lambda a, b: set(a) & set(b), dataframe_list)
common
>Solution :
from itertools import combinations
out = {(a, b): set(dic[a])&set(dic[b]) for a,b in combinations(dic, r=2)}
Or as a more generic code (useful if you need combinations of more than 2):
from itertools import combinations
from operator import itemgetter
out = {x: set.intersection(*map(set, itemgetter(*x)(dic)))
for x in combinations(dic, r=2)}
# or
from itertools import combinations
out = {x: set.intersection(*(set(dic[k]) for k in x))
for x in combinations(dic, r=3)}
Output:
{('df1', 'df2'): {2, 3, 4, 5, 555},
('df1', 'df3'): {1, 2, 3, 4, 5, 555},
('df2', 'df3'): {2, 3, 4, 5, 6, 555}}
You can also simplify your original code to find the intersection of all values:
common = set.intersection(*map(set, dic.values()))
Output: {2, 3, 4, 5, 555}