Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
807
reputation
6
10

abarnert

Some people, when confronted with a problem, think "I know, I'll use itertools."

Now they have next(takewhile(not_, count())) problems.


Since I've been asked about this multiple times now:

It's a reference to "Now they have two problems" about regular expressions. next(takewhile(not_, count())) is guaranteed to give you 0, and pretty efficiently… but it's not exactly readable. Which is the joke. itertools does solve all your problems, but you don't always need to use it.

If you don't grasp the paradigm, go read David Beazley's presentations. Once you do: "I've got a CSV, and I want to group the rows by…" Pass a csv.reader to groupby. "I've got a bunch of values, and I want to take them 4 at a time and…" Use grouper from the recipes. "I've got a file, and I want to take each pair of adjacent lines and…" tee it, next one copy, and zip them. And so on.

But of course some problems have an even easier solution, and once you start thinking in terms of sequences of transformations on iterators, sometimes you'll miss the easier answer.

(The exact same thing happens with numpy, of course.)

5
answers
3
questions
~67k
people reached

Top Tags (20)

Score 38
Posts 4
Posts % 50
Score 34
Posts 2
Score 34
Posts 1
Score 34
Posts 1
Score 15
Posts 2
Score 11
Posts 4

Top Posts (8) All Questions Answers | Votes Newest

Badges (16)

Gold

Silver

6

Rarest

Bronze

10

Rarest