diff lists
Alex Martelli
aleaxit at yahoo.com
Wed Mar 28 14:49:25 EST 2001
"Oliver Vecernik" <vecernik at aon.at> wrote in message
news:3AC1DD4E.4BA7D421 at aon.at...
> Hi,
>
> 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?
No doubt, it's a loop testing with the 'in' operator if one list is
pretty small, one using an auxiliary dictionary if they're more
substantial:
def smallExcluded(big, small):
return [ x for x in big if x not in small ]
def anyExcluded(big, any):
auxdict = {}
for x in any: auxdict[x] = None
return [ x for x in big if not auxdict.has_key(x) ]
There are also interesting approaches when the lists are known
to be sorted, but I guess those would be of purely theoretical
interest to you:-).
Alex
More information about the Python-list
mailing list