Fate of itertools.dropwhile() and itertools.takewhile()

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Dec 30 03:29:44 EST 2007


On Sat, 29 Dec 2007 15:10:24 -0800, Raymond Hettinger wrote:

> These thoughts reflect my own experience with the itertools module.
> It may be that your experience with them has been different.  Please
> let me know what you think.

I seem to be in a minority here as I use both functions from time to time.
One "recipe" is extracting blocks from text files that are delimited by a
special start and end line.

def iter_block(lines, start_marker, end_marker):
    return takewhile(lambda x: not x.startswith(end_marker),
                     dropwhile(lambda x: not x.startswith(start_marker),
                               lines))

Maybe these functions usually don't turn up in code that can be called
"recipes" so often but are useful for themselves.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list