By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Filtering a Python list to uniques

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.