I'd like to know how to elegantly check a list for the membership of
any of its items to another list. Not caring for elegance, I would
use the following code:
That's one of the useful properties of sets:
>>> a = [1,2,3]
>>> b = [3,4,5,6]
>>> set(a) & set(b)
set([3])
>>> set(a).intersection(b)
set([3])
That's two spellings of the same thing. As for testing: an empty set like an empty list will return false, so "if set(a) & set(b):" will be true or false based on if there's any commonalities between the two lists.
--Stephen
signature.asc
Description: OpenPGP digital signature
-- http://mail.python.org/mailman/listinfo/python-list