[Python-ideas] list…pushed, or something

Shane Green shane at umbrellacode.com
Fri Mar 29 05:32:33 CET 2013


Yes, that would be a nice function.  The unique items from a ordered sequence was just supposed to be an example of something you could do with the concept I was going for: push/add methods that returned the value pushed/added, so a 'list.pushed(value)' could replace value inline.  Of course it's turning out the unique items may best example which, in and of itselft, isn't very compelling argument for my original idea because there are better ways to solve this problem, as you've pointed out.  



On Mar 28, 2013, at 8:15 AM, MRAB <python at mrabarnett.plus.com> wrote:

> 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]
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas




More information about the Python-ideas mailing list