Tim Chase <[EMAIL PROTECTED]> wrote:
   ...
> To get the item, i had to resort to methods that feel less than 
> the elegance I've come to expect from python:
> 
>       >>> item = [x for x in s][0]

A shorter, clearer expression of the same idea:

item = list(s)[0]

or

item = list(s).pop()

> or the more convoluted two-step
> 
>       >>> item = s.pop()
>       >>> s.add(item)

which in turn suggests

item = set(s).pop()

Similar ideas include iter(s).next() and s.copy().pop().

Basically: s has no way to get the item non-destructively, so, either
make a copy (and use the destructive-get 'pop' on the copy) or build
from s a type which DOES have ways to get the item (iterator, list, etc)
be they destructive or not.  As for speed, measuring is the only way,
and timeit is your friend.  As for elegance, the most concise readable
form is "set(s).pop()" and that's what I would use.


Alex


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to