This works, but it doesn't preserve the ordering of the sublists:
def bygroup(k):
k = sorted(sorted(x) for x in k)
return [k for k,_ in itertools.groupby(k)]
>>> k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [2, 1]]
>>> bygroup(k)
[[1, 2], [2, 5, 6], [3], [4]]
In Python 2.7 or 3.2, you could use an OrderedDict if you need to preserve the order within sublists and also the general order of the list (except for duplicates), but it's much slower:
def bydict(k):
s = collections.OrderedDict()
for i in k:
s[tuple(sorted(i))] = i
return s.values()
>>> bydict(k)
[[2, 1], [4], [5, 6, 2], [3]]
I tested with 100,000 iterations using timeit. The bydict function took about 4 times longer in Python 2.7.2 and about 3 times longer in Python 3.2.