from itertools import combinations lstA = ['Harry Potter','1984','50 Shades','Dracula'] lstB = ['50 Shades','Dracula','1984','Harry Potter'] setA = set(combinations(lstA, 2)) setB = set(combinations(lstB, 2)) result = setA - setB print(result) lstB[1] = 'The Godfather' setA = set(combinations(lstA, 2)) setB = set(combinations(lstB, 2)) resultA = setA - setB resultB = setB.difference(x[::-1] for x in setA) result = resultA | resultB print(result)
Standard input is empty
{('Harry Potter', '1984'), ('1984', '50 Shades'), ('Harry Potter', '50 Shades'), ('Harry Potter', 'Dracula'), ('1984', 'Dracula')} {('50 Shades', 'The Godfather'), ('Harry Potter', '50 Shades'), ('The Godfather', 'Harry Potter'), ('Harry Potter', '1984'), ('1984', 'Dracula'), ('The Godfather', '1984'), ('1984', '50 Shades'), ('50 Shades', 'Dracula'), ('Harry Potter', 'Dracula')}