[FRPythoneers] using pop() on a list

Jack Diederich jack at performancedrivers.com
Thu Jan 2 20:21:31 MST 2003


If you don't need the items that you are poping from
the list, just use filter()

def not_four(item):
  if (item != 4):
    return 1
  else:
    return 0

mylist = filter(not_four, mylist)

If you prefer lambdas (I do) it would look like
mylist = filter(lambda x:x != 4, mylist)
lambdas don't work for complicated tests, typically.

If you need both the discarded and original
lists, you're best bet is the while loop
someone described earlier.

i = 0
are_four = []
while (i < len(mylist)):
  if (mylist[i] == 4):
    are_four.push(mylist.pop(i))
  else:
    i += 1

-jack



More information about the FRPythoneers mailing list