How do you find common sublists between two lists?
list, python, set, sublist
Solution
Better change the list of lists to list of tuples, then you can easily use the set operations:
>>> tupa = map(tuple, lsta)
>>> tupb = map(tuple, lstb)
>>> set(tupa).intersection(tupb)
set([('a', 'b', 'c'), ('e', 'f', 'g')])
>>> set(tupa).difference(tupb)
set([('c', 'd', 'e')])
Problem
How do you find or keep only the sublists of a list if it the sublist is also present within another list? ``` lsta = [['a','b','c'],['c','d','e'],['e','f','g']] lstb = [['a','b','c'],['d','d','e'],['e','f','g']] ``` I'd like to do something like set(lsta) & set(lstb) ``` Desired_List = [['a','b','c'],['e','f','g']] ``` The reason I'd like to do something like set is for it's speed as I'm doing this on a very large list where efficiency is quite important. Also, slightly unrelated, what if I wanted to subtract lstb from lsta to get ``` Desired_List2 = [['d','d','e']] ```