469,358 Members | 1,518 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Filtering a Python list to uniques

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
ke*********@nospam.com
change nospam to yahoo
Mar 25 '08 #1
6 35739
Kelly Greer:
What is the best way to filter a Python list to its unique members?
If Python is "batteries included", then an industrial-strength
unique() seems one of the most requested 'batteries' that's not
included :-) I feel that it's coming in Python 2.6/3.x. In the
meantime:
http://aspn.activestate.com/ASPN/Coo.../Recipe/502263
http://aspn.activestate.com/ASPN/Coo.../Recipe/438599

Bye,
bearophile
Mar 25 '08 #2
On Mar 25, 6:30*pm, kellygreer1 <kellygre...@yahoo.comwrote:
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...@nospam.com
change nospam to yahoo
Mar 25 '08 #3
On Mar 25, 4:30*pm, kellygreer1 <kellygre...@yahoo.comwrote:
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 ]
If the elements are hashable, try this:
lsttwo = sorted(set(lstone))
If not hashable, try:
lsttwo = [k for k,v in itertools.groupby(sorted(lstone))]
Raymond
Mar 26 '08 #4
On 26 อมา, 02:30, kellygreer1 <kellygre...@yahoo.comwrote:
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...@nospam.com
change nospam to yahoo

or just look this thread for a fastest solution
http://groups.google.com/group/comp....8961f1ed91ccea
Mar 26 '08 #5
On Mar 26, 5:45 am, hellt <Dodin.Ro...@gmail.comwrote:
On 26 อมา, 02:30,kellygreer1<kellygre...@yahoo.comwrote:
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...@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
Mar 26 '08 #6
On Wed, Mar 26, 2008 at 2:50 PM, kellygreer1 <ke*********@yahoo.comwrote:
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?
Some of the elements of tmp are unhashable. Unhashable items can't be
the keys of a dictionary or members of a set. I don't think you've
said anywhere in the thread what these items are, you just started out
with an example of a list of integers. Do you believe the elements in
tmp are integers? If so, try the following -

for v in tmp:
print type(v), repr(v), hash(v)

and let us know what it spits out.

--
Jerry
Mar 26 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Dave Brueck | last post: by
1 post views Thread by Dave Brueck | last post: by
3 posts views Thread by Brad Clements | last post: by
reply views Thread by Heiko Wundram | last post: by
reply views Thread by Chris | last post: by
9 posts views Thread by Terry Reedy | last post: by
8 posts views Thread by jerry.levan | last post: by
1 post views Thread by Tom Stambaugh | last post: by
reply views Thread by Gareth Owen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.