[Python-Dev] Retrieve an arbitrary element from a set without removing it

Scott David Daniels Scott.Daniels at Acm.Org
Mon Oct 26 07:14:06 CET 2009


John Arbash Meinel wrote:
> res = heads(node1, node2)
> if len(res) == 1:
>   # What is the 'obvious' way to get the node out?
> 
> I posit that there *isn't* an obvious way to get the single item out of
> a 1-entry frozenset.
> 
> for x in res: break
> list(res)[0]
> set(res).pop()
> iter(res).next()
> [x for x in res][0]
> x, = res   # I didn't think of this one before recently
> 
> Are all answers, but none of them I would consider *obvious*. 
And from my SQL-hacking experience:

     x = min(s)

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-Dev mailing list