def flatten ( nested , containers = ( list , tuple ) ) : for item in nested : if hasattr ( item , "next" ) or isinstance ( item , containers ) : for subitem in flatten ( item ) : yield subitem else : yield item
python flatten list or tuples
Flatten a nested list by yielding its scalar items .
def flatten(nested, containers=(list, tuple)):
    """ Flatten a nested list by yielding its scalar items.
    """
    for item in nested:
        if hasattr(item, "next") or isinstance(item, containers):
            for subitem in flatten(item):
                yield subitem
        else:
            yield item