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.

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 <>
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)
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: <>
2020-09-25 01:20:19terry.reedylinkissue41774 messages
2020-09-25 01:20:19terry.reedycreate