1-D intersection of lines given begin and end points I have a fairly basic question here: I want to find if two lines on a 1-D plane intersect. I know of two simple ways to solve this, but I wanted to know if Python has a more elegant way to solve this.

x = [1, 10] # 1 = begin, 10 = end
y = [15, 20]
z = [5, 12]

#Method 1: Works. Is quick. Lots of typing.
def is_intersect_1(a, b):
    bool_check = False
    if a[0] &lt;= b[0] &lt;= a[1] or \
    a[0] &lt;= b[1] &lt;= a[1] or \
    b[0] &lt;= a[0] &lt;= b[1] or \
    b[0] &lt;= a[1] &lt;= b[1]:
        bool_check = True
    return bool_check

is_intersect_1(x,y) # False
is_intersect_1(x,z) # True

#Method 2: Quicker to write. Simpler to read. Uses more memory and is slower.

def is_intersect_2(a, b):
    bool_check = False
    if set(range(a[0], a[1]+1)).intersection(set(range(b[0], b[1])):
        bool_check = True
    return bool_check

is_intersect_2(x,y) # False
is_intersect_2(x,z) # True
Using explicit booleans is a common antipattern:

check = False
if condition:
    check = True
return check


is equivalent to much more transparent (and preferable)

return condition

Segments do not intersect if a lies completely to the left, or completely to the right of b. Assuming that segments are themselves sorted:

def is_intersect_3(a, b):
    return not (a[1] &lt; b[0] or a[0] &gt; b[1])

is_intersect_2 fails badly on floats