Unique Elements in a List

Edvard Majakari edvard+news at majakari.net
Tue May 10 03:53:35 EDT 2005


James Stroud <jstroud at mbi.ucla.edu> writes:

> from sets import Set
>
> data = [0.1,0.5,0.6,0.4,0.1,0.5,0.6,0.9]
>
> [x for x in Set(data) if data.count(x) == 1]

Um.

...I must have missed something, but I'll post nevertheless:

wouldn't just

[x for x in data if data.count(x) == 1]

suffice? it is also "stable"  preserving order of items. Lemme demo:

>>> [x for x in Set(data) if data.count(x) == 1]
[0.90000000000000002, 0.40000000000000002]

>>> [x for x in data if data.count(x) == 1]
[0.40000000000000002, 0.90000000000000002]

Though I'll admit I also thought of Sets first, because I didn't remember
there was such a nice method list.count().

-- 
# Edvard Majakari		Software Engineer
# PGP PUBLIC KEY available    	Soli Deo Gloria!
One day, when he was naughty, Mr Bunnsy looked over the hedge into Farmer
Fred's field and it was full of fresh green lettuces. Mr Bunnsy, however, was 
not full of lettuces. This did not seem fair.  --Mr Bunnsy has an adventure 



More information about the Python-list mailing list