Filtering a Python list to uniques

kellygreer1 kellygreer1 at yahoo.com
Wed Mar 26 19:50:30 CET 2008


On Mar 26, 5:45 am, hellt <Dodin.Ro... at gmail.com> wrote:
> On 26 ÍÁÒ, 02:30,kellygreer1<kellygre... at yahoo.com> wrote:
>
> > What is the best way to filter a Python list to its unique members?
> > I tried some method using Set but got some "unhashable" error.
>
> > lstone = [ 1, 2, 3, 3, 4, 5, 5, 6 ]
> > # how do i reduce this to
> > lsttwo = [ 1, 2, 3, 4, 5, 6 ]
>
> > Is there a page on this in the Python in a Nutshell or the Python
> > Cookbook?
> > Did I miss something?
>
> > Kelly Greer
> > kellygre... at nospam.com
> > change nospam to yahoo
>
> or just look this thread for a fastest solutionhttp://groups.google.com/group/comp.lang.python/browse_frm/thread/709...

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?

Thanks,
Kelly



More information about the Python-list mailing list