Good use for itertools.dropwhile and itertools.takewhile

Nick Mellor thebalancepro at gmail.com
Tue Dec 4 14:57:58 CET 2012


Hi,

I came across itertools.dropwhile only today, then shortly afterwards found Raymond Hettinger wondering, in 2007, whether to drop [sic] dropwhile and takewhile from the itertools module.

Fate of itertools.dropwhile() and itertools.takewhile() - Python
bytes.com
http://bit.ly/Vi2PqP

Almost nobody else of the 18 respondents seemed to be using them.

And then 2 hours later, a use case came along. I think. Anyone have any better solutions?

I have a file full of things like this:

"CAPSICUM RED fresh from Queensland"

Product names (all caps, at start of string) and descriptions (mixed case, to end of string) all muddled up in the same field. And I need to split them into two fields. Note that if the text had said:

"CAPSICUM RED fresh from QLD"

I would want QLD in the description, not shunted forwards and put in the product name. So (uncontrived) list comprehensions and regex's are out.

I want to split the above into:

("CAPSICUM RED", "fresh from QLD")

Enter dropwhile and takewhile. 6 lines later:

from itertools import takewhile, dropwhile
def split_product_itertools(s):
    words = s.split()
    allcaps = lambda word: word == word.upper()
    product, description = takewhile(allcaps, words), dropwhile(allcaps, words)
    return " ".join(product), " ".join(description)


When I tried to refactor this code to use while or for loops, I couldn't find any way that felt shorter or more pythonic:

(9 lines: using for)

def split_product_1(s):
    words = s.split()
    product = []
    for word in words:
        if word == word.upper():
            product.append(word)
        else:
            break
    return " ".join(product), " ".join(words[len(product):])


(12 lines: using while)

def split_product_2(s):
    words = s.split()
    i = 0
    product = []
    while 1:
        word = words[i]
        if word == word.upper():
            product.append(word)
            i += 1
        else:
            break
    return " ".join(product), " ".join(words[i:])


Any thoughts?

Nick



More information about the Python-list mailing list