How to extract the member from single-member set in python?

Tuple unpacking works.

(element,) = myset

(By the way, python-dev has explored but rejected the addition of myset.get() to return an arbitrary element from a set. Discussion here, Guido van Rossum answers 1 and 2.)

My personal favorite for getting an arbitrary element is (when you have an unknown number, but also works if you have just one):

element = next(iter(myset)) ยน

1: in Python 2.5 and before, you have to use iter(myset).next()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)