Pythonic infinite for loop?
Paul Rubin
no.email at nospam.invalid
Fri Apr 15 03:47:34 EDT 2011
Chris Angelico <rosuav at gmail.com> writes:
>> sentinel = object()
>> seq = (dct.get('Keyword%d'%i,sentinel) for i in count(1))
>> lst = list(takewhile(lambda x: x != sentinel, seq))
>
> If I understand this code correctly, that's creating generators,
> right? It won't evaluate past the sentinel at all?
Right, it should stop after hitting the sentinel once.
> That might well be what I'm looking for. A bit ugly, but efficient and
> compact. And I can bury some of the ugliness away.
It occurs to me, operator.ne might be a little faster than the
interpreted lambda.
More information about the Python-list
mailing list