Delete common entries between two dictionaries

David Eppstein eppstein at ics.uci.edu
Mon Nov 24 17:44:37 EST 2003


In article <WGvwb.7230$9O5.2236 at fed1read06>,
 "Amy G" <amy-g-art at cox.net> wrote:

> How do I do this same thing but with lists???
> 
> I apparently have two lists... not dictionaries.
> 
> This is what it prints if I add
> print domains_black

domains_black = [x for x in domains_black if x not in domains_white]

If domains_white is a long list, this will be inefficient due to the 
linear search to test whether each x belongs to it.  In that case, you 
might be better off using a set:

mask = Set(domains_white)
domains_black = [x for x in domains_black if x not in mask]

Also, this creates a new list.  If you instead want to change the same 
list in-place, you could replace "domains_black =" with
"domains_black[:] =".

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list