[Python-ideas] list…pushed, or something
MRAB
python at mrabarnett.plus.com
Thu Mar 28 16:15:52 CET 2013
On 28/03/2013 05:28, Bruce Leban wrote:
>
> On Wed, Mar 27, 2013 at 10:11 PM, Shane Green <shane at umbrellacode.com
> <mailto:shane at umbrellacode.com>> wrote:
>
> [seen.added(value) for value in sequence if value not in seen] *
>
>
> Here's an easy way to do it:
>
> >>> seen = set()
> >>> seq = [3,2,1,2,3,4,5,4]
> >>> [seen.add(v) or v for v in seq if v not in seen]
> [3, 2, 1, 4, 5]
> >>> seen
> {1, 2, 3, 4, 5}
>
I think I would prefer a "unique" function that yields unique items:
def unique(items):
seen = set()
for item in items:
if item not in seen:
seen.add(item)
yield item
>>> seq = [3,2,1,2,3,4,5,4]
>>> list(unique(seq))
[3, 2, 1, 4, 5]
More information about the Python-ideas
mailing list