How to find the intersection and union of two lists in Python
My friend Bill had previously alerted me to the coolness of Python
set
s. However I hadn't found opportunity to use them
until now. Here are three functions using set
s to
remove duplicate entries from a list, find the intersection of two
lists, and find the union of two lists. Note, set
s were
introduced in Python 2.4, so Python 2.4 or later is required. Also,
the items in the list must be hashable and order of the lists is not
preserved.
For more information on Python set
s, see the
Library Reference.
""" NOTES:
- requires Python 2.4 or greater
- elements of the lists must be hashable
- order of the original lists is not preserved
"""
def unique(a):
""" return the list with duplicate elements removed """
return list(set(a))
def intersect(a, b):
""" return the intersection of two lists """
return list(set(a) & set(b))
def union(a, b):
""" return the union of two lists """
return list(set(a) | set(b))
if __name__ == "__main__":
a = [0,1,2,0,1,2,3,4,5,6,7,8,9]
b = [5,6,7,8,9,10,11,12,13,14]
print unique(a)
print intersect(a, b)
print union(a, b)
Results:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [8, 9, 5, 6, 7] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Related posts
- An example using groupby and defaultdict to do the same task — posted 2014-10-09
- python enum types — posted 2012-10-10
- Python data object motivated by a desire for a mutable namedtuple with default values — posted 2012-08-03
- How to sort a list of dicts in Python — posted 2010-04-02
- Python setdefault example — posted 2010-02-09
- How to conditionally replace items in a list — posted 2008-08-22
Comments
You may also want to check out this: [http://stackoverflow.com/questions/642763/python-intersection-of-two-lists]
Derek: Thanks. My method is simple, but it has its limitations.
thanks for the above - very helpful. do you also know a good solution for intersection & union without eliminating the duplicates beforehand? say I have
a = [1,1,2,4,4]
b = [1,4,4,4]
I would like to get
for intersection = [1,4,4]
and for union = [1,1,2,4,4,4].
Right now, I cannot think of anything better than two nested loops.
Thanks.
Thank you. It was exactly what I was looking for!
@panta,
How about this:
def to_multiset(x):
result = set()
max_rep = len(x)
for elt in x:
for n in xrange(max_rep):
n_elt = (elt,n)
if n_elt not in result:
result.add(n_elt)
break
return result
def from_multiset(x):
return sorted([elt for elt,n in x])
def multi_union(a, b):
aa = to_multiset(a)
bb = to_multiset(b)
return from_multiset(aa | bb)
def multi_intersect(a, b):
aa = to_multiset(a)
bb = to_multiset(b)
return from_multiset(aa & bb)
a = [1, 1, 2, 4, 4]
b = [1, 4, 4, 4]
expected_intersection = [1, 4, 4]
expected_union = [1, 1, 2, 4, 4, 4]
print multi_union(a, b), expected_union
print multi_intersect(a, b), expected_intersection