Removing None objects from a sequence

Terry Reedy tjreedy at udel.edu
Fri Dec 12 17:23:33 EST 2008


If you want to literally remove None objects from a list....(or mutable 
sequence)

def deNone(alist):
   n=len(alist)
   i=j=0
   while i < n:
     if alist[i] is not None:
       alist[j] = alist[i]
       j += 1
     i += 1
   alist[j:i] = []

blist=[None,1,None,2,None,3,None,None,4,None]
deNone(blist)
print(blist)

# prints [1, 2, 3, 4]

Terry Jan Reedy




More information about the Python-list mailing list