Closed 0f33552e-3426-4ce7-83ad-4a88a6ffc812 closed 7 years ago
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
Another possible way to go is to expand the existing tee() object to support __getitem and __bool methods.
I like the suggested API: "iterator with indexing" is a good articulation of the request.
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).
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = 'https://github.com/rhettinger' closed_at =
created_at =
labels = ['type-feature', 'library']
title = 'Add lookahead/peek wrapper to itertools'
updated_at =
user = 'https://github.com/phmc'
```
bugs.python.org fields:
```python
activity =
actor = 'rhettinger'
assignee = 'rhettinger'
closed = True
closed_date =
closer = 'rhettinger'
components = ['Library (Lib)']
creation =
creator = 'pconnell'
dependencies = []
files = ['29606', '29616']
hgrepos = []
issue_num = 17577
keywords = []
message_count = 4.0
messages = ['185527', '185604', '185681', '281538']
nosy_count = 4.0
nosy_names = ['rhettinger', 'terry.reedy', 'ezio.melotti', 'pconnell']
pr_nums = []
priority = 'low'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue17577'
versions = ['Python 3.4']
```