Good use for itertools.dropwhile and itertools.takewhile

Vlastimil Brom vlastimil.brom at
Tue Dec 4 22:08:34 CET 2012

2012/12/4 Nick Mellor <thebalancepro at>:
> I love the way you guys can write a line of code that does the same as 20 of mine :)
> I can turn up the heat on your regex by feeding it a null description or multiple white space (both in the original file.) I'm sure you'd adjust, but at the cost of a more complex regex.
> Meanwhile takewith and dropwith are behaving themselves impeccably but my while loop has fallen over.
> Best,
> Nick
>> [...]
> --

well, for what is it worth, both cases could be addressed quite
easily, with little added complexity - e.g.: make the description part
optional, allow multiple whitespace and enforce word boundary after
the product name in order to get rid of the trailing whitespace in it:

>>> re.findall(r"(?m)^([A-Z\s]+\b)(?:\s+(.*))?$", "CAPSICUM RED fresh from QLD\nCAPSICUM    RED   fresh from    Queensland\nCAPSICUM RED")
[('CAPSICUM RED', 'fresh from QLD'), ('CAPSICUM    RED', 'fresh from
 Queensland'), ('CAPSICUM RED', '')]

However, it's certainly preferable to use a solution you are more
comfortable with, e.g. the itertools one...


More information about the Python-list mailing list