best way to compare contents of 2 lists?

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Apr 24 07:20:16 EDT 2009


Arnaud Delobelle:
> Thanks to the power of negative numbers, you only need one dict:
>
> d = defaultdict(int)
> for x in a:
>     d[x] += 1
> for x in b:
>     d[x] -= 1
> # a and b are equal if d[x]==0 for all x in d:
> not any(d.itervalues())

Very nice, I'll keep this for future use.
Someday I'll have to study this new new kind of numbers that can
represent borrowed items too.

Bye,
bearophile



More information about the Python-list mailing list