Re:Python idiom for taking the single item from a list
by robrenaud on Monday September 27, 2010 @10:45PM
(#1734359)
They also have the side effect of removing an item from the collection.
[ Parent
| on HN ]
Re:Python idiom for taking the single item from a list
by lehmannro on Tuesday September 28, 2010 @06:14AM
(#1735168)
A property which is exposed by the article's solution too:
>>> x = (lambda: (yield 1))() # generator with one step
>>> y = tuple(x)[0]
>>> y
1
>>> list(x) # exhausted
[]
>>> x = (lambda: (yield 1))()
>>> y, = x
>>> y
1
>>> list(x) # exhausted, too!
[]
Because that's just what you inevitably need to do to fetch a value from a generator. There is no peeking action or some such.