3

Is there a way to remove duplicate sublists from a list of lists, even if they are not the same ordering?

So could I do something like make:

x = [[1,2],[3,4],[5,6],[2,1],[7,8]]

into

x = [[1,2],[3,4],[5,6],[7,8]]

Is there an itertools function or something with a for loop?

Thanks!

5
  • Will you ever have to distinguish [1,1,2] from [1,2,1]? And do you need to preserve the initial order of the sublists? Commented Dec 9, 2013 at 3:40
  • 2
    Also, is the order of the returned list significant? Commented Dec 9, 2013 at 3:40
  • @DSM yes, the length of the sublists may increase. And no, the order is not important. Commented Dec 9, 2013 at 3:44
  • @DSM, but the order within the sublists is important, so [7,8] is not the same as [8,7]. Commented Dec 9, 2013 at 3:50
  • @DSM Yes, that is what I mean. If for a sublist there is a matching one elsewhere in the list, then one, preferably the second, should be removed. But, for a sublist which does not have a duplicate, it should remain the same. Commented Dec 9, 2013 at 3:55

3 Answers 3

4

this will preserve the order of list and sublists, with possible duplicates in sublists:

y, s = [], set()
for t in x:
    w = tuple(sorted(t))
    if not w in s:
        y.append(t)
        s.add(w)

if

x = [[1,2],[3,4],[5,6],[2,1,1],[2,1],[7,8],[4,3],[1,2,1]]

then y will be:

[[1, 2], [3, 4], [5, 6], [2, 1, 1], [7, 8]]
Sign up to request clarification or add additional context in comments.

1 Comment

+1: This preserves as much original information as possible (unlike my OrderedDict approach), and seems to satisfy the OP's desiderata.
1

You can use frozenset:

>>> def remove_dups(L):
        return map(list, frozenset(map(frozenset, L)))

>>> x = [[1,2],[3,4],[5,6],[2,1],[7,8]]
>>> remove_dups(x)
[[5, 6], [1, 2], [8, 7], [3, 4]]
>>> 

Comments

0

try this:

a=[[1,2],[3,4],[5,6],[2,1],[7,8]]
y=[]
for i in a:
        if sorted(i) not in y:
                y.append(i)
print y

output is

[[1, 2], [3, 4], [5, 6], [7, 8]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.