This is a quality algo improvement. I will note that the del can just be "del myList[i_write:]" without the manually calculated upper bound.
On Sun, May 20, 2018, 23:39 MrGumm <stabbingfin...@gmail.com> wrote: > I like this one. I would modify somewhat. If you don't like "not" you > could change the function name to keep_item() and reverse the logic. > > i_write = 0for item in myList: > if not needsToBeDeleted(item): > myList[i_write] = item > i_write += 1del myList[i_write:len(myList)] > > > On 5/20/2018 6:20 PM, Ian Mallett wrote: > > On Sun, May 20, 2018 at 5:55 PM, Irv Kalb <i...@furrypants.com> wrote: > >> Is there a way to do that without the list comprehension? I'm building >> this as part of a class, and I will not have talked about list >> comprehensions up until that point. >> > > A list comprehension is the clearest and most-pythonic way to do this (it > would have been my suggestion if Daniel hadn't beat me to it). > > You could modify and unroll the initial algorithm you started with to make > it work. It's a lot less pretty, but it's clear algorithmically (which is > the point pedagogically). It also has the advantage of being the most > efficient algorithm for deletion in an array (no reallocation should be > necessary, and copying is minimized). > > i_read = 0 > i_write = 0 > while i_read < len(myList): > item = myList[i_read] > i_read += 1 > if needsToBeDeleted(item): > pass > else: > myList[i_write] = item > i_write += 1 > while len(myList) > i_write: > myList.pop() > > Ian > > >