This PEP has been rejected.enumerate(), introduced inPEP 279,covers the use-case proposed in this PEP, and the PEP author has beenunreachable.
This PEP describes the often proposed feature of exposing the loopcounter in for-loops. This PEP tracks the status and ownership ofthis feature. It contains a description of the feature andoutlines changes necessary to support the feature. This PEPsummarizes discussions held in mailing list forums, and providesURLs for further information, where appropriate. The CVS revisionhistory of this file contains the definitive historical record.
Standard for-loops in Python iterate over the elements of asequence[1]. Often it is desirable to loop over the indices orboth the elements and the indices instead.
The common idioms used to accomplish this are unintuitive. ThisPEP proposes two different ways of exposing the indices.
The current idiom for looping over the indices makes use of thebuilt-inrange function:
foriinrange(len(sequence)):# work with index i
Looping over both elements and indices can be achieved either by theold idiom or by using the newzip built-in function[2]:
foriinrange(len(sequence)):e=sequence[i]# work with index i and element e
or:
fori,einzip(range(len(sequence)),sequence):# work with index i and element e
There are three solutions that have been discussed. One adds anon-reserved keyword, the other adds two built-in functions.A third solution adds methods to sequence objects.
indexingThis solution would extend the syntax of the for-loop by addingan optional<variable>indexing clause which can also be usedinstead of the<variable>in clause.
Looping over the indices of a sequence would thus become:
foriindexingsequence:# work with index i
Looping over both indices and elements would similarly be:
foriindexingeinsequence:# work with index i and element e
indices andirangeThis solution adds two built-in functionsindices andirange.The semantics of these can be described as follows:
defindices(sequence):returnrange(len(sequence))defirange(sequence):returnzip(range(len(sequence)),sequence)
These functions could be implemented either eagerly or lazily andshould be easy to extend in order to accept more than one sequenceargument.
The use of these functions would simplify the idioms for loopingover the indices and over both elements and indices:
foriinindices(sequence):# work with index ifori,einirange(sequence):# work with index i and element e
This solution proposes the addition ofindices,itemsandvalues methods to sequences, which enable looping overindices only, both indices and elements, and elements onlyrespectively.
This would immensely simplify the idioms for looping over indicesand for looping over both elements and indices:
foriinsequence.indices():# work with index ifori,einsequence.items():# work with index i and element e
Additionally it would allow to do looping over the elementsof sequences and dictionaries in a consistent way:
foreinsequence_or_dict.values():# do something with element e
For all three solutions some more or less rough patches existas patches at SourceForge:
foriindexingainl: exposing the for-loop counter[3]indices() andirange() to built-ins[4]items() method to listobject[5]All of them have been pronounced on and rejected by the BDFL.
Note that theindexing keyword is only aNAME in thegrammar and so does not hinder the general use ofindexing.
As no keywords are added and the semantics of existing coderemains unchanged, all three solutions can be implementedwithout breaking existing code.
This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-0212.rst
Last modified:2025-02-01 08:55:40 GMT