def flatten(inp):
    """
    The function to flatten a nested list or tuple of lists, tuples or ranges,
    with any number of nested levels.
    From StackOverflow by James Brady (http://stackoverflow.com/users/29903/james-brady)
    http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python/406822#406822
    """
    out = []
    for el in inp:
        if isinstance(el, (list, tuple, range)):
            out.extend(flatten(el))
        else:
            out.append(el)
    return out