Since you donβt care about order, you can easily remove duplicates by subtracting and converting to a list. Here he is in a one-line monster:
>>> mylist = [ ... [1, 2, 3, 4], ... [2, 5, 6, 7], ... [4, 2, 8, 9] ... ] >>> mynewlist = [list(set(thislist) - set(element for sublist in mylist for element in sublist if sublist is not thislist)) for thislist in mylist] >>> mynewlist [[1, 3], [5, 6, 7], [8, 9]]
Note: This is not very effective because duplicates are recounted for each row. Whether this will be a problem or not depends on your data size.
source share