Slow comparison between two lists
Hrvoje Niksic
hniksic at xemacs.org
Thu Oct 23 08:19:26 EDT 2008
Jani Tiainen <redetin at gmail.com> writes:
> for addr in list_external:
> if addr not in list_internal:
> addr.status = 1 # New address
>
> But in my case running that loop takes about 10 minutes. What I am
> doing wrong?
The nested loop takes time proportional to the product of the number
of elements in the loops. To avoid it, convert the inner loop to a
set, which can be looked up in constant time:
internal = set(list_internal)
for addr in list_external:
if addr in internal:
addr.status = 1
More information about the Python-list
mailing list