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

Rene Pijlman at
Wed Jan 25 16:21:42 CET 2006

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


>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?

More information about the Python-list mailing list