Membership of multiple items to a list

inkhorn matt.dubins at sympatico.ca
Sun Feb 1 15:06:54 EST 2009


Wow thanks for the lightning fast reply!  This does exactly the right
job.

Matt

On Feb 1, 3:01 pm, Stephen Hansen <apt.shan... at gmail.com> wrote:
> 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
> < 1KViewDownload




More information about the Python-list mailing list