Filtering a Python list to uniques

Mensanator mensanator at aol.com
Tue Mar 25 19:52:57 EDT 2008


On Mar 25, 6:30 pm, 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?

I don't know, the set() soution worked for me.

>>> lstone = [1,2,3,3,4,5,5,6]
>>> setone = set(lstone)
>>> lsttwo = list(setone)
>>> lstone
[1, 2, 3, 3, 4, 5, 5, 6]
>>> setone
set([1, 2, 3, 4, 5, 6])
>>> lsttwo
[1, 2, 3, 4, 5, 6]


>
> Kelly Greer
> kellygre... at nospam.com
> change nospam to yahoo




More information about the Python-list mailing list