Test if two lists overlap

Date: April 30th 2016
Last updated: April 30th 2016

Use sets and intersection.

# create overlapping lists
l1=[1,2,3]
l2=[3,4,5]

# get elements in common between lists
set(l1).intersection(l2)
# {3}

# test if an element is returned
bool(set(l1).intersection(l2))
# True

# create non-overlapping list
l3=[4,5,6]

# get elements in common (returns empty set)
set(l1).intersection(l3)
# set()

# empty set returns False
bool(set(l1).intersection(l3))
# False

results matching ""

    No results matching ""