# 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:** 1\
**Showing post:** 22

<div class="post-metadata">

**Author:** ![eendebakpt](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eendebakpt/32/7552_2.png) [@eendebakpt](https://discuss.python.org/u/eendebakpt)\
**Post date:** [April 23, 2024, 10:32pm UTC](https://discuss.python.org/t/nwise-itertools/51718/22 "2024-04-23T22:32:23Z")

</div>

The reason the current C implementation `pairwise` is slower than the python recipe seems to be the usage of `Py_TuplePack` (as was already hinted at by @Stefan2 in the linked discussion on pairwise and zip).

Replacing `Py_TuplePack` (which uses varargs) with a direct `Py_TuplePack2` makes the `pairwise` C implementation as fast as the pure python recipe. Branch: [Comparing python:main...eendebakpt:putuple\_pack2 · python/cpython · GitHub](https://github.com/python/cpython/compare/main...eendebakpt:cpython:putuple_pack2)

The `Py_TuplePack` with arguments 1 and 2 is used several times in the Python codebase, so perhaps we can add `Py_TuplePack1` and `Py_TuplePack2` to the python API. I will create an issue to discuss that a bit later.

Update: issue is at [Add Py\_TuplePack2 and Py\_TuplePack1 · Issue #118222 · python/cpython · GitHub](https://github.com/python/cpython/issues/118222)

---

_[View the full topic](https://discuss.python.org/t/nwise-itertools/51718)._
