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 lookahead/peek wrapper to itertools
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: ezio.melotti, pconnell, rhettinger, terry.reedy
Priority: low Keywords:

Created on 2013-03-29 23:37 by pconnell, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
lookahead.py pconnell, 2013-03-29 23:37 Pure Python implementation
peekiter.py rhettinger, 2013-03-31 03:29 Alternate Lookahead API with __getitem__ and __bool__ API
Messages (4)
msg185527 - (view) Author: Phil Connell (pconnell) * Date: 2013-03-29 23:37
A recipe often requested on the likes of stackoverflow and activestate is a way to look 'ahead' in an iterator, without altering the values returned for subsequent next() calls.

Last time this came up on python-ideas, it got a reasonable reception:
  http://code.activestate.com/lists/python-ideas/19509/

So, having wanted (and implemented) this again, I thought it was worth a formal proposal.

Is this an idea worth pursuing?


I've attached a quick pure Python implementation, that adds a 'peek' method, used like this:

>>> it = lookahead(range(10))
>>> next(it)
0
>>> it.peek()
1
>>> next(it)
1
>>> it.peek(index=1)
3
>>> it.peek()
2
>>> next(it)
2
msg185604 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-03-31 01:00
Another possible way to go is to expand the existing tee() object to support __getitem__ and __bool__ methods.
msg185681 - (view) Author: Phil Connell (pconnell) * Date: 2013-03-31 21:38
I like the suggested API: "iterator with indexing" is a good articulation of the request.
msg281538 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-11-23 06:43
This doesn't seem to have gained any traction and I haven't interest in the subject for years.  Marking this as closed.  If the topic takes on a new life, this can be reopened and we can revisit the idea.

I don't think it would be hard to patch itertools.tee() to support indexing modeled on the patch above, but it is unclear whether it would be useful in practice or whether it would just be an attractive nuisance.  (Note, we added copy support to tee() based on similar requests however this feature turned out to be ignored in practice).
History
Date User Action Args
2022-04-11 14:57:43adminsetgithub: 61777
2016-11-23 06:43:14rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg281538
2013-04-01 02:17:40rhettingersetmessages: - msg185534
2013-04-01 02:17:22rhettingersetmessages: - msg185533
2013-03-31 21:38:52pconnellsetmessages: + msg185681
2013-03-31 03:30:00rhettingersetfiles: - peekiter.py
2013-03-31 03:29:47rhettingersetfiles: + peekiter.py
2013-03-31 01:00:14rhettingersetmessages: + msg185604
2013-03-30 23:12:18rhettingersetfiles: + peekiter.py
2013-03-30 01:06:05rhettingersetmessages: + msg185534
stage: needs patch ->
2013-03-30 01:05:10rhettingersetpriority: normal -> low
assignee: rhettinger
messages: + msg185533
2013-03-29 23:41:21ezio.melottisetnosy: + ezio.melotti

stage: needs patch
2013-03-29 23:37:54pconnellcreate