Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 608
Sanjivani Avatar asked Feb 23 '13 04:02

Sanjivani


2 Answers

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]]
like image 177
mgilson Avatar answered Oct 20 '22 01:10

mgilson


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]]
like image 37
Abhijit Avatar answered Oct 20 '22 03:10

Abhijit