Pythonic infinite for loop?

Paul Rubin at nospam.invalid
Fri Apr 15 09:47:34 CEST 2011

Chris Angelico <rosuav at> 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, might be a little faster than the
interpreted lambda.

More information about the Python-list mailing list