list_a-list_b

Geoff Gerrietts geoff at gerrietts.net
Tue Apr 16 20:38:45 EDT 2002


Quoting John (nobody at nobody.com):
> 
> list_a=[1,2,4], list_b=[2,4]
> list_minus(list_a,list_b)
> print list_a #will print out a=[1] after removing all elements ([2,4])
> appearing in list_b

Lots of solutions for this problem, many of which are more general
than others. The simplest way is probably:

def list_minus(a, b):
  return [ x for x in a if not in b ]

That's probably not the best, if your measure of best is "takes the
least amount of time", once the lists get relatively large. But it's
probably acceptable for most cases, especially due to its relative
compactness and simplicity.

Thanks,
--G.

-- 
Geoff Gerrietts                <geoff at gerrietts net>
"I have read your book and much like it." --Moses Hadas





More information about the Python-list mailing list