Sanjivani
Sanjivani

Reputation: 273

Python: Remove Duplicate Items from Nested list

mylist = [[1,2],[4,5],[3,4],[4,3],[2,1],[1,2]]

I want to remove duplicate items, duplicated items can be reversed. The result should be :

mylist = [[1,2],[4,5],[3,4]]

How do I achieve this in Python?

Upvotes: 6

Views: 8942

Answers (3)

Oleg
Oleg

Reputation: 1559

If order is not important:

def rem_dup(l: List[List[Any]]) -> List[List[Any]]:
    tuples = map(lambda t: tuple(sorted(t)), l)
    return [list(t) for t in set(tuples)]

Upvotes: 0

Abhijit
Abhijit

Reputation: 63707

If the Order Matters you can always use OrderedDict

>>> unq_lst = OrderedDict()
>>> for e in lst:
    unq_lst.setdefault(frozenset(e),[]).append(e)


>>> map(list, unq_lst.keys())
[[1, 2], [4, 5], [3, 4]]

Upvotes: 2

mgilson
mgilson

Reputation: 309831

lst=[[1,2],[4,5],[3,4],[4,3],[2,1],[1,2]]
fset = set(frozenset(x) for x in lst)
lst = [list(x) for x in fset]

This won't preserve order from your original list, nor will it preserve order of your sublists.

>>> lst=[[1,2],[4,5],[3,4],[4,3],[2,1],[1,2]]
>>> fset = set(frozenset(x) for x in lst)
>>> lst = [list(x) for x in fset]
>>> lst
[[1, 2], [3, 4], [4, 5]]

Upvotes: 13

Related Questions