an element from a set

Raymond Hettinger python at rcn.com
Mon May 17 11:43:23 EDT 2010


On May 14, 3:24 pm, gerardob <gberbeg... at gmail.com> wrote:
> Hello, let S be a python set which is not empty
> (http://docs.python.org/library/sets.html)
>
> i would like to obtain one element (anyone, it doesn't matter which one) and
> assign it to a variable.
>
> How can i do this?

x = next(iter(s))

or you can supply a default value in case the set is empty:

x = next(iter(s), default_value)


Raymond



More information about the Python-list mailing list