Intersect Two Lists in Python

3 Jun 2010

Very often I find myself combining lists, removing duplicates, or looking for differences or intersects of sets. I have no idea how I have never discovered how useful the set type in python is.

Here is a really simple way to find the intersect of two lists/sets:

>>> b1 = [1,2,3,4,5,9,11,15]
>>> b2 = [4,5,6,7,8]
>>> set(b1).intersection( set(b2) )
set([4, 5])

via Stack Overflow via Google.

Let's talk

I'm one of the founders of Venmo. If you liked this essay/experiment -- or didn't -- @ me.