How to get an item from a simple set?

Steven Bethard steven.bethard at gmail.com
Wed Nov 24 11:32:56 EST 2004


Pete Forman wrote:
> I actually wanted to append the single item to a string,  Steven's
> solutions work for assignment.
[snip]
>>>>line = 'bar '
>>>>line += iter(s).next()
>>>>line
> 'bar foo'

Yeah, using the assignment's an extra line:

 >>> line_list = ['bar ']
 >>> item, = s
 >>> line_list.append(item)
 >>> ''.join(line_list)
'bar foo'

I still tend to write the extra line in cases like this -- it guarantees 
that the set is really the size that I think it is, where the 
iter(s).next() solution will not raise an exception if the set is 
actually larger.

Steve



More information about the Python-list mailing list