Find included range from a range given excluded range in python
Question: I have a range say (0,1000). My another input will be excluded range
[(200,400), (600,800)]. I need as output included range [(0,199), (401,599),
(801,1000)].

How do I implement this in python?


Answer: You can [chain
iterables](https://docs.python.org/2/library/itertools.html#itertools.chain)
together:

    
    
    from itertools import chain
    
    for i in chain(range(1,200), range(401, 600), range(801, 1001)):
        # etc
    

Also, if you've been given the range and can only iterate over it once, you
can use
[**`tee`**](https://docs.python.org/2/library/itertools.html#itertools.tee)
and
[**`islice`**](https://docs.python.org/2/library/itertools.html#itertools.islice):

    
    
    def excluded(sequence):
        first, second, third = tee(sequence, 3)
        yield from chain(islice(first, 1, 200),
                         islice(second, 401, 600),
                         islice(third, 801, 1001))
    
    for i in excluded(range(0, 1000)):
        # etc.
    

