Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python eliminate duplicates of list with unhashable elements in one line [duplicate]

Possible Duplicate:
Python: removing duplicates from a list of lists

Say i have list

a=[1,2,1,2,1,3]

If all elements in a are hashable (like in that case), this would do the job:

list(set(a))

But, what if

a=[[1,2],[1,2],[1,3]]

?

like image 204
Sanich Avatar asked May 28 '12 12:05

Sanich


People also ask

How do you remove duplicates from a list while keeping the same order of the elements?

If you want to preserve the order while you remove duplicate elements from List in Python, you can use the OrderedDict class from the collections module. More specifically, we can use OrderedDict. fromkeys(list) to obtain a dictionary having duplicate elements removed, while still maintaining order.

How do I remove multiple duplicates from a list in Python?

If the order of the elements is not critical, we can remove duplicates using the Set method and the Numpy unique() function. We can use Pandas functions, OrderedDict, reduce() function, Set + sort() method, and iterative approaches to keep the order of elements.

How do I remove one duplicate from a list in Python?

The method unique() from Numpy module can help us remove duplicate from the list given. The Pandas module has a unique() method that will give us the unique elements from the list given. The combination of list comprehension and enumerate is used to remove the duplicate elements from the list.

How do you remove duplicates from a list using loops?

Using for-loop To remove duplicates using for-loop , first you create a new empty list. Then, you iterate over the elements in the list containing duplicates and append only the first occurrence of each element in the new list. The code below shows how to use for-loop to remove duplicates from the students list. Voilà!


1 Answers

Python 2

>>> from itertools import groupby
>>> a = [[1,2],[1,2],[1,3]]
>>> [k for k,v in groupby(sorted(a))]
[[1, 2], [1, 3]]

Works also in Python 3 but with caveat that all elements must be orderable types.

like image 60
jamylak Avatar answered Sep 18 '22 20:09

jamylak