Author terry.reedy
Recipients eric.smith, paul.moore, rhettinger, sreedevi.ha, steven.daprano, terry.reedy
Date 2020-09-25.01:20:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1600996819.72.0.798610721769.issue41774@roundup.psfhosted.org>
In-reply-to
Content
You are right; the replacement index I called 'j' is better buried as a C index or pointer within a slice replacement. In fact, a generator expression, if one has a keep expression, or a filter call, if one has filter function, work, without the intermediate list.   Both also incorporate the keep scan index/pointer in C.  I verified that this works by defining 3 functions.

def fc(n, keep):
    mylist = list(range(n))
    mylist[:] = [x for x in mylist if keep(x)]
    return mylist

def fg(n, keep):
    mylist = list(range(n))
    mylist[:] = (x for x in mylist if keep(x))
    return mylist

def fl(n, keep):
    mylist = list(range(n))
    mylist[:] = filter(keep, mylist)
    return mylist

I added a second test expression.

    print(fc(i, keep) == fg(i, keep) == fl(i, keep) == expect)

at the 3 obvious places in the test loop above.
---

In the existing question about removing duplicates, the existing all-hashable answer
   mylist = list(set(mylist))
could be replaced by
   mylist[:] = set(mylist)
History
Date User Action Args
2020-09-25 01:20:19terry.reedysetrecipients: + terry.reedy, rhettinger, paul.moore, eric.smith, steven.daprano, sreedevi.ha
2020-09-25 01:20:19terry.reedysetmessageid: <1600996819.72.0.798610721769.issue41774@roundup.psfhosted.org>
2020-09-25 01:20:19terry.reedylinkissue41774 messages
2020-09-25 01:20:19terry.reedycreate