# Nwise itertools

**URL:** <https://discuss.python.org/t/nwise-itertools/51718>\
**Category:** Ideas\
**Created:** [April 23, 2024, 3:44pm UTC](https://discuss.python.org/t/nwise-itertools/51718 "2024-04-23T15:44:45Z")\
**Posts on this page:** 10\
**Page:** 3

<div class="post-metadata">

**Author:** ![alicederyn](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alicederyn/32/10282_2.png) [@alicederyn](https://discuss.python.org/u/alicederyn)\
**Post date:** [April 30, 2024, 7:17am UTC](https://discuss.python.org/t/nwise-itertools/51718/43 "2024-04-30T07:17:52Z")

</div>

> [@pylang](#):
>
> The point of the examples was to indicate the popularity of the sliding window concept, not about…specific algorithms,

This seems pretty important though. If the proposed algorithm is of the wrong complexity for the cited popular use-cases then it sounds like it would be misleading at best to include it in the standard library?

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [April 30, 2024, 11:25am UTC](https://discuss.python.org/t/nwise-itertools/51718/44 "2024-04-30T11:25:31Z")

</div>

> [@pylang](#):
>
> The point of the examples was to indicate the popularity of the sliding window concept, not about timings nor even specific algorithms, although I see the latter part of this thread is somehow focused on optimization, so I understand the confusion.

But that first example is about a _different_ sliding window concept, one whose entire point is optimization. Where using the itertool proposed here would be _detrimental_ or not even work at all.

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [May 7, 2024, 3:36pm UTC](https://discuss.python.org/t/nwise-itertools/51718/45 "2024-05-07T15:36:56Z")

</div>

I agree with opinion that if this was to be added, it would be more beneficial to have full `more_itertools.windowed` functionality.

For my personal use, I found that even that one is not “complete” in functionality for my needs, so I ended up adding one more argument:

```python
def windowed(seq, n, fillvalue=None, step=1, n_partials=Auto):
    """
    Args:
        seq : list, tuple
        n_partials : int
            Auto: behaviour as per mitl.windowed
              -1: will do maximum partial elements
    """
    ...

In [14]: btl.windowed([1, 2, 3], 4)
Out[14]: [[1, 2, 3, None]]

In [15]: btl.windowed([1, 2, 3], 4, n_partials=0)
Out[15]: []

In [16]: btl.windowed([1, 2, 3], 4, n_partials=1)
Out[16]: [[1, 2, 3, None]]

In [17]: btl.windowed([1, 2, 3], 4, n_partials=2)
Out[17]: [[1, 2, 3, None], [2, 3, None, None]]

In [18]: btl.windowed([1, 2, 3], 4, n_partials=3)
Out[18]: [[1, 2, 3, None], [2, 3, None, None], [3, None, None, None]]

In [19]: btl.windowed([1, 2, 3], 4, n_partials=4)
Out[19]: [[1, 2, 3, None], [2, 3, None, None], [3, None, None, None]]

```

---

<div class="post-metadata">

**Author:** ![bbayles](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bbayles/32/8415_2.png) [@bbayles](https://discuss.python.org/u/bbayles)\
**Post date:** [May 7, 2024, 9:05pm UTC](https://discuss.python.org/t/nwise-itertools/51718/46 "2024-05-07T21:05:57Z")

</div>

For what it’s worth, I (the `more-itertools` maintainer) wrote the original `more_itertools.windowed` function. In retrospect, I regret adding the `fillvalue` and `step` parameters - I should have just put in examples where I composed a solution out of the other itertools.

I won’t take them away - lots of projects use `windowed`, and I won’t break them even with a major version change. But I wish I hadn’t complicated the code originally!

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [May 8, 2024, 2:15am UTC](https://discuss.python.org/t/nwise-itertools/51718/47 "2024-05-08T02:15:15Z")

</div>

> [@bbayles](#):
>
> I should have just put in examples where I composed a solution out of the other itertools.

That makes sense. Didn’t think there were good (performant and concise) solutions for it. Would you be kind and share solution emulating the full behaviour composed of other tools?

---

<div class="post-metadata">

**Author:** ![bbayles](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bbayles/32/8415_2.png) [@bbayles](https://discuss.python.org/u/bbayles)\
**Post date:** [May 9, 2024, 10:13am UTC](https://discuss.python.org/t/nwise-itertools/51718/48 "2024-05-09T10:13:33Z")

</div>

For the fill values, you can pad the input with `chain` and `repeat`. `step` is more complex, but the code [here](https://github.com/more-itertools/more-itertools/blob/7b5319da74ba371d6c21d57848caed399305616b/more_itertools/more.py#L865-L870) uses `islice`, `map`, and `chain`.

---

<div class="post-metadata">

**Author:** ![lsproule](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/lsproule/32/19420_2.png) [@lsproule](https://discuss.python.org/u/lsproule)\
**Post date:** [May 10, 2024, 4:34pm UTC](https://discuss.python.org/t/nwise-itertools/51718/50 "2024-05-10T16:34:25Z")

</div>

Sorry I have been busy with school. I want to write up a bit of a better proposal, and see what we can do to fit everyone who has commented’s opinions in the spec. I will try to see if we can have a performance testing plan as well, so we can be more sure of the implementation.

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [May 10, 2024, 6:50pm UTC](https://discuss.python.org/t/nwise-itertools/51718/51 "2024-05-10T18:50:21Z")

</div>

> [@bbayles](#):
>
> For the fill values, you can pad the input with `chain` and `repeat`. `step` is more complex, but the code [here](https://github.com/more-itertools/more-itertools/blob/7b5319da74ba371d6c21d57848caed399305616b/more_itertools/more.py#L865-L870) uses `islice`, `map`, and `chain`.

Thanks for this. This is much nicer recipe than the current one IMO. Unfortunately, wasn’t able to extend it for `n_partials` parameter. At least not yet.

Apologies for mixing in another thread, but I think [Itertools.ilen(iterable)](https://discuss.python.org/t/itertools-ilen-iterable/53002) is part of the same book. I am curious what is your opinion on `ilen` addition to `itertools` (from the perspective of `more_itertools` maintainer)?

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [May 10, 2024, 11:25pm UTC](https://discuss.python.org/t/nwise-itertools/51718/52 "2024-05-10T23:25:27Z")

</div>

> [@bbayles](#):
>
> In retrospect, I regret adding the `fillvalue` and `step` parameters - I should have just put in examples where I composed a solution out of the other itertools.

I have played around with your recipe and it works very well. I even managed to add parameter that I needed.

However, it still seems that it is difficult to wrap `windowed` function without arguments to provide them from outside without complicating matters more than they currently are.

---

<div class="post-metadata">

**Author:** ![bbayles](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bbayles/32/8415_2.png) [@bbayles](https://discuss.python.org/u/bbayles)\
**Post date:** [May 13, 2024, 4:55pm UTC](https://discuss.python.org/t/nwise-itertools/51718/53 "2024-05-13T16:55:18Z")

</div>

Re: `ilen`, I’m happy to have it in `more-itertools`. I probably won’t add any extra arguments or features to it.

[Previous page](https://discuss.python.org/t/nwise-itertools/51718.md?page=2)
