# given that I've got Python2.3.[45] on hand,
# hack the following two lines to get a "set" object
>>> import sets
>>> set = sets.Set
>>> s = set(['test'])
>>> len(s)
1
>>> s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unindexable object
(which is kinda expected, given that it's unordered...an index
doesn't make much sense)
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]
or the more convoluted two-step
>>> item = s.pop()
>>> s.add(item)
or even worse, intruding into private members
>>> item = s._data.keys()[0]
Is any of these more "pythonic" than the others? Is there a more
elegant 2.3.x solution? If one upgrades to 2.4+, is there
something even more elegant? I suppose I was looking for
something like
>>> item = s.aslist()[0]
which feels a little more pythonic (IMHO). Is one solution
preferred for speed over others (as this is happening in a fairly
deeply nested loop)?
Any tips, preferences, input, suggestions, pointers to obvious
things I've missed, or the like?
Thanks,
-tkc
> I suppose I was looking for something like
>
> >>> item = s.aslist()[0]
>
> which feels a little more pythonic (IMHO). Is one solution
> preferred for speed over others (as this is happening in a fairly
> deeply nested loop)?
the obvious solution is
item = list(s)[0]
but that seems to be nearly twice as slow as [x for x in s][0]
under 2.4. hmm.
here's a faster variant:
item = iter(s).next()
but at least on my machine, your two-step solution
item = s.pop(); s.add(item)
seems to be even faster.
</F>
> When you have a set, known to be of length one, is there a "best"
> ("most pythonic") way to retrieve that one item?
>>> s = set(["one-and-only"])
>>> item, = s
>>> item
'one-and-only'
This works for any iterable and guarantees that it contains exactly one
item. The comma may easily be missed, though.
Peter
Fuzzyman
e = s.copy().pop() #:-)
--
René Pijlman
Wat wil jij worden? http://www.carrieretijger.nl
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
Nice!
>The comma may easily be missed, though.
You could write:
(item,) = s
But I'm not sure if this introduces additional overhead.
Funny, and true on my laptop too:
helen:~ alex$ python -mtimeit -s's=set([23])' 'x=list(s)[0]'
100000 loops, best of 3: 2.55 usec per loop
helen:~ alex$ python -mtimeit -s's=set([23])' 'x=[x for x in s][0]'
100000 loops, best of 3: 1.48 usec per loop
helen:~ alex$ python -mtimeit -s's=set([23])' '[x for x in s]'
1000000 loops, best of 3: 1.36 usec per loop
Exploiting the design defect whereby a LC leaves variables bound can
shave another few percents off it, as shown.
> here's a faster variant:
>
> item = iter(s).next()
Not all that fast here:
helen:~ alex$ python -mtimeit -s's=set([23])' 'x=iter(s).next()'
100000 loops, best of 3: 1.71 usec per loop
> but at least on my machine, your two-step solution
>
> item = s.pop(); s.add(item)
> seems to be even faster.
Not really, here:
helen:~ alex$ python -mtimeit -s's=set([23])' 'x=s.pop();s.add(x)'
100000 loops, best of 3: 1.49 usec per loop
No joy from several variations on transform-and-pop:
helen:~ alex$ python -mtimeit -s's=set([23])' 'x=set(s).pop()'
100000 loops, best of 3: 2.21 usec per loop
helen:~ alex$ python -mtimeit -s's=set([23])' 'x=list(s).pop()'
100000 loops, best of 3: 3.2 usec per loop
helen:~ alex$ python -mtimeit -s's=set([23])' 'x=tuple(s)[0]'
100000 loops, best of 3: 1.79 usec per loop
Fastest I've found is unpacking-assignment:
helen:~ alex$ python -mtimeit -s's=set([23])' 'x,=s'
1000000 loops, best of 3: 0.664 usec per loop
Alex
you can make this a bit more obvious:
>>> [item] = s
this is almost twice as fast as the fastest alternative from my previous
post.
</F>
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
And that is no coincidence. All three variants are compiled to the same
bytecode:
>>> import dis
>>> def a(): x, = s
...
>>> def b(): (x,) = s
...
>>> def c(): [x] = s
...
>>> dis.dis(a)
1 0 LOAD_GLOBAL 0 (s)
3 UNPACK_SEQUENCE 1
6 STORE_FAST 0 (x)
9 LOAD_CONST 0 (None)
12 RETURN_VALUE
>>> dis.dis(b)
1 0 LOAD_GLOBAL 0 (s)
3 UNPACK_SEQUENCE 1
6 STORE_FAST 0 (x)
9 LOAD_CONST 0 (None)
12 RETURN_VALUE
>>> dis.dis(c)
1 0 LOAD_GLOBAL 0 (s)
3 UNPACK_SEQUENCE 1
6 STORE_FAST 0 (x)
9 LOAD_CONST 0 (None)
12 RETURN_VALUE
Peter
That's probably because of the name lookup needed for "list"
> helen:~ alex$ python -mtimeit -s's=set([23])' 'x=[x for x in s][0]'
> 100000 loops, best of 3: 1.48 usec per loop
> helen:~ alex$ python -mtimeit -s's=set([23])' '[x for x in s]'
> 1000000 loops, best of 3: 1.36 usec per loop
>
> Exploiting the design defect whereby a LC leaves variables bound can
> shave another few percents off it, as shown.
>
>
>>here's a faster variant:
>>
>> item = iter(s).next()
>
>
> Not all that fast here:
>
> helen:~ alex$ python -mtimeit -s's=set([23])' 'x=iter(s).next()'
> 100000 loops, best of 3: 1.71 usec per loop
That's probably because of the 2 name lookups needed :) "iter" and "next"