[Tutor] synchronized enumeration

Chris or Leslie Smith smiles at worksmail.net
Fri Dec 16 16:29:42 CET 2005


Has anyone else run into the desire to synchronize the indices that are being used during an enumeration with the true indices of the list that is being enumerated when you use a slice of the list?

e.g. more than a couple of times, I want to use the enumeration function, but I don't want to start at the beginning of a list. I do something like:

###
for i, x in enumerate(aList[3:]):
        pass #do something with the index and or x

###

Of course, if I do something with x in this case, there is no problem, but (often enough) I forget that the index 0 that is returned by enumerate is actually corresponding to index 3 (b/c that's where I started the slice).

What I would love to see--and is there a chance of this being considered--is something like the following behavior for enumerate:

###
def enumerate(l, start=None, stop=None, step=None, alwaysPos = False):
    if step==None:step=1
    if start==None:
        if step<0:
            start=-1
        else:
            start=0
    for i, dat in enumerate(l[start:stop:step]):
        j = i*step+start
        if alwaysPos and j<0: j+=len(l)
        yield j, dat

for i, x in enumerate(range(5),3):
    print i, x
###

which gives output:

3 3
4 4

rather than enumerate(range(5)[3:])'s output of

0 3
1 4

Any thoughts?

/c


More information about the Tutor mailing list