This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add an intersperse function to itertools
Type: enhancement Stage:
Components: Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: cvrebert, eric.araujo, javawizard, rhettinger
Priority: normal Keywords:

Created on 2014-05-02 07:39 by javawizard, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg217746 - (view) Author: Alexander Boyd (javawizard) Date: 2014-05-02 07:39
Itertools would benefit greatly from a function (which I've named intersperse, after Haskell's equivalent) for yielding the items of an iterator with a given value placed between each. Sort of a str.join-like function, but for arbitrary sequences.

Something like:

>>> list(itertools.intersperse(1, [2, 3, 4]))
[2, 1, 3, 1, 4]
msg217764 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2014-05-02 17:29
I looked for a previous discussion of this on the Python mailing lists and found nothing.

Existing solutions include https://twitter.com/snim2/status/393821419114483712 and http://stackoverflow.com/questions/5655708/python-most-elegant-way-to-intersperse-a-list-with-an-element

Raymond is the one to decide whether this should be added.  If he doesn't comment in the near future, feel free to propose this on python-ideas to see if people like it.  You may also want to ask maintainers of projects like more-itertools if they have a similar function and if people use it.
msg217814 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-05-03 04:33
What is the use case for interspersing values?  I've not run across anything like it ever before.
msg217854 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-05-04 02:13
I've looked into this a bit more.  Even in Haskell, it seems that use case is for str.join() which we already have.  I don't see intersperse() in any other functional languages.  That suggests that its use cases aren't sufficiently common to warrant adding it to the standard library.

Thank you for the submission.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65613
2014-05-04 02:13:34rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg217854
2014-05-03 04:33:21rhettingersetassignee: rhettinger
messages: + msg217814
2014-05-02 23:14:58terry.reedysetversions: + Python 3.5
2014-05-02 17:29:32eric.araujosetnosy: + rhettinger, eric.araujo
messages: + msg217764
2014-05-02 16:43:01cvrebertsetnosy: + cvrebert
2014-05-02 07:39:41javawizardcreate