an element from a set
Terry Reedy
tjreedy at udel.edu
Fri May 14 12:39:43 EDT 2010
On 5/14/2010 11:24 AM, gerardob 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?
Depends on whether or not you want the element removed from the set
#3.1
>>> s=set(range(10))
>>> s
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> x=next(iter(s))
>>> x
0
>>> s
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} # x not removed
>>> x = s.pop()
>>> x
0
>>> s
{1, 2, 3, 4, 5, 6, 7, 8, 9} # x has been removed
The choice of 0 is an implementation artifact. It could have been any
member.
Terry Jan Reedy
More information about the Python-list
mailing list