Best way to extract an item from a set of len 1
Alex Martelli
aleax at mail.comcast.net
Thu Jan 26 00:20:02 EST 2006
Rene Pijlman <reply.in.the.newsgroup at my.address.is.invalid> wrote:
> Peter Otten:
> >>>> s = set(["one-and-only"])
> >>>> item, = s
...
> >The comma may easily be missed, though.
>
> You could write:
>
> (item,) = s
>
> But I'm not sure if this introduces additional overhead.
Naah...:
helen:~ alex$ python -mtimeit -s's=set([23])' 'x,=s'
1000000 loops, best of 3: 0.689 usec per loop
helen:~ alex$ python -mtimeit -s's=set([23])' '(x,)=s'
1000000 loops, best of 3: 0.652 usec per loop
helen:~ alex$ python -mtimeit -s's=set([23])' '[x]=s'
1000000 loops, best of 3: 0.651 usec per loop
...much of a muchness.
Alex
More information about the Python-list
mailing list