Filtering a Python list to uniques

Gabriel Genellina gagsl-py2 at
Wed Mar 26 23:29:19 CET 2008

En Wed, 26 Mar 2008 15:50:30 -0300, kellygreer1 <kellygreer1 at>  

> On Mar 26, 5:45 am, hellt <Dodin.Ro... at> wrote:
>> On 26 ÍÁÒ, 02:30,kellygreer1<kellygre... at> wrote:
>> > What is the best way to filter a Python list to its unique members?

> How come the Set() thing seems to work for some people and I get the
> 'unhashable' error?
> How do you test for 'membership' on a dictionary?
> # where tmp is the non-unique list
> # dct is a dictionary where each unique key will be tied to a count
> (the value)
> # for testing I was setting the count to 0
> for v in tmp:
>     if not v in dct: dct[v] = 0
> # I get unhashable error here.
> # Even if I write it.
> for v in tmp:
>     if not v in dct.keys(): dct[v] = 0
> What am I missing?

Mutable objects can't be used as keys in a dict nor be members of a set  
(at least if the mutable part is used to implement the == comparison).  
Technically, they should not implement __hash__ so hash(x) fails on them -  
they are "unhashable".

Objects that can be used as keys in a dictionary or be members of a set  
instances of all numeric types, strings, tuples, frozensets; all of them  
are immutable and hashable. Instances of user-defined classes that don't  
implement __eq__ nor __cmp__ nor __hash__ are hashable and can be used  
too. Classes that implement such special methods must ensure that (x==y)  
=> (hash(x)==hash(y)) and in that case can be used too; else, they should  
not implement __hash__ at all.

On the other hand, mutable containers can't be keys in a dict, nor be  
members of a set; including instances of lists, sets, dicts, all of them  
are mutable and unhashable objects.

Read the Cookbook recipes that someone posted before; one of them is  
generic enough to handle all cases, trying the fastest approaches first.

If you think that all your objects should be hashable, try to find which  
one isn't.

Gabriel Genellina

More information about the Python-list mailing list