Best way to extract an item from a set of len 1

Rene Pijlman reply.in.the.newsgroup at my.address.is.invalid
Wed Jan 25 10:21:42 EST 2006


Peter Otten:
>>>> s = set(["one-and-only"])
>>>> item, = s
>>>> item
>'one-and-only'
>
>This works for any iterable and guarantees that it contains exactly one
>item. 

Nice!

>The comma may easily be missed, though.

You could write:

    (item,) = s

But I'm not sure if this introduces additional overhead.

-- 
René Pijlman

Wat wil jij worden?  http://www.carrieretijger.nl



More information about the Python-list mailing list