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: itertools.getitem()
Type: Stage:
Components: Extension Modules Versions: Python 2.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: doerwalter, rhettinger
Priority: normal Keywords: patch

Created on 2007-07-08 09:33 by doerwalter, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
diff.txt doerwalter, 2007-07-08 09:33
Messages (2)
msg52812 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2007-07-08 09:33
This patch adds itertools.getitem() which is basically equivalent to the following python code:

_default = object()

def getitem(iterable, index, default=_default):
   try:
      return list(iterable)[index]
   except IndexError:
      if default is _default:
         raise
      return default

but without materializing the complete list. Negative indexes are supported too.
msg52813 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-07-31 07:03
Rejected after an offline discussion with the OP.

The use case for getitem(n) with n as a negative number depended on an unlikely combination of circumstances:

* you have an iterable that is not a sequence (otherwise, just use s[-n]).
* the iterable is somewhat large (otherwise, just list it into memory)
* the iterable is finite (otherwise, getitem() dies in a infinite loop)
* you only want one entry (otherwise, you'll have make multiple passes)
* that entry is located near the end (otherwise getitem() is memory intensive)
* you know the entry's offset from the end but not from the beginning
* identifying the record of interest depends only on its position, not its content

In the common case where the index is zero, the preferred spelling is to use the next() method -- that is its purpose.  For cases where the index is a positive integer, uing islice(it, n).next() suffices though it doesn't have the cute feature for a default value.

History
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45158
2007-07-08 09:33:32doerwaltercreate