How to get an item from a simple set?

Steven Bethard steven.bethard at gmail.com
Wed Nov 24 10:40:30 EST 2004


Pete Forman wrote:
> I have a set that contains one item.  What is the best way of getting
> at that item?  Using pop() empties the set.  Here is what I've tried.

This is what tuple unpacking is for:

 >>> s = set(['foo'])
 >>> item, = s
 >>> item
'foo'
 >>> [item] = s
 >>> item
'foo'

It's up to you whether you like the tuple or list syntax better. =)

Steve



More information about the Python-list mailing list