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

Paul Rubin http
Thu Jan 10 22:05:10 EST 2008


Raymond Hettinger <python at rcn.com> writes:
> > I presume you did scans of
> > large code bases and you did not find occurrences of
> > takewhile and dropwhile, right?
> 
> Yes.

I think I have used them.  I don't remember exactly how.  Probably
something that could have been done more generally with groupby.

I remember a clpy thread about a takewhile gotcha, that it consumes an
extra element:

    >>> from itertools import takewhile as tw
    >>> x = range(10)
    >>> z = iter(x)
    >>> list(tw(lambda i:i<5, z))
    [0, 1, 2, 3, 4]
    >>> z.next()
    6

I.e. I had wanted to use takewhile to split a list into the
initial sublist satisfying some condition, and the rest of the
list.

This all by itself is something to at least warn about.  I don't
know if it's enough for deprecation.

I've been cooking up a scheme for iterators with lookahead, that I
want to get around to coding and posting.  It's a harder thing
to get right than it at first appears.



More information about the Python-list mailing list