diff lists

Remco Gerlich scarblac at pino.selwerd.nl
Thu Mar 29 05:04:49 EST 2001


Oliver Vecernik <vecernik at aon.at> wrote in comp.lang.python:
> I've got following two lists:
> 
> ['a', 'b', 'c', 'd', 'e', 'f']
> ['e', 'c', 'f']
> 
> I'd like to have the result:
> 
> ['a', 'b', 'd']
> 
> The list need not to be ordered. ['d', 'a', 'b'] will also be ok. What
> is the most effective way to achive that result?

If your lists are going to be small, speed won't be an issue and you can't
beat

c = [element for element in a if element not in b]

for readability, in my opinion. This will usually be fast enough. If the
second list gets huge though, the problem is that 'element not in b' scans
the whole second list if necessary for every element of a. If speed is an
issue, then maybe something with a dictionary like

dict = {}
for element in a:
   dict[element] = element
for element in b:
   if dict.has_key(element): 
      del dict[element]
c = dict.keys() # Is dict.values() faster?

Test the two with lists typical for your program (see the profile.run
function).

-- 
Remco Gerlich



More information about the Python-list mailing list