Message377471
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:19 | terry.reedy | set | recipients:
+ terry.reedy, rhettinger, paul.moore, eric.smith, steven.daprano, sreedevi.ha |
2020-09-25 01:20:19 | terry.reedy | set | messageid: <1600996819.72.0.798610721769.issue41774@roundup.psfhosted.org> |
2020-09-25 01:20:19 | terry.reedy | link | issue41774 messages |
2020-09-25 01:20:19 | terry.reedy | create | |
|