[Python-ideas] [Python-Dev] Proposal: new list function: pack

Nick Coghlan ncoghlan at gmail.com
Sat Mar 21 05:35:12 CET 2009

Josiah Carlson wrote:
> iwinslice() is just as bad of a name as any of the others.
> I have seen the equivalent of window(iterator, size=2, step=1), which
> works as you would expect (both as the output, as well as the
> implementation), with size and step both limited to 5 (because if you
> are doing things with more than 5 items at a time...you probably
> really want something else, and in certain cases, you can use multiple
> window calls to compose larger groups).

Oops, I didn't realise this thread had moved over here, so I just
repeated what yourself and Raymond said over on python-dev. Oh well...

> I'd be a -0 on the feature, because as Raymond says, it's trivial to
> implement with a deque.  And as I've said before, not all x line
> functions should be built-in.

That does raise the possibility of adding "iterator windowing done
right" by including a deque based implementation in itertools (or at
least in the itertools recipes page).

For example, the following continuously yields the same deque, but each
time the contents represent a new window onto the underlying data:

  from collections import deque
  def window (iterable, size=2, step=1, overlap=0):
    itr = iter(iterable)
    new_per_window = size - overlap
    contents = deque(islice(itr, 0, size*step, step), size)
    while True:
      yield contents
      new_data = list(islice(itr, 0, new_per_window*step, step))
      if len(new_data) < new_per_window:

(There are other ways of doing it that involve less data copying, but
the above way seems to be the most straightforward)


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list