You can use sum and generator expression:
>>> sum(not a <= value <= b for a, b in ranges_array) 1
a and b are the lower and upper bounds of the range, respectively. a <= value <= b is a chain comparison , which is equivalent to a <= value and value <= b . The final return value is the number of times the expression not a <= value <= b evaluates to True .
We can get a slightly longer, but more readable solution if we apply the laws of De Morgan:
>>> sum(value < a or value > b for a, b in ranges_array) 1
Technically, you can use range , as shown in other answers, but they will be slower in Python 3 (due to the creation of the range object) and much slower in Python 2 (since Python 2 range creates the actual list and list containment checks are O (n )):
$ python3.6 -m timeit -s "a, b, value = 0, 1000, 500" "a <= value <= b" 10000000 loops, best of 3: 0.0343 usec per loop $ python3.6 -m timeit -s "a, b, value = 0, 1000, 500" "value in range(a, b + 1)" 1000000 loops, best of 3: 0.28 usec per loop $ python2.7 -m timeit -s "a, b, value = 0, 1000, 500" "value in range(a, b + 1)" 100000 loops, best of 3: 7.97 usec per loop