flatten generator rewritten as a plain function : Yield : Statement PYTHON TUTORIALS


PYTHON TUTORIALS » Statement » Yield »

 

flatten generator rewritten as a plain function


def flatten(nested):
    result = []
    try:
        try: nested + ''
        except TypeError: pass
        else: raise TypeError
        for sublist in nested:
            for element in flatten(sublist):
                result.append(element)
    except TypeError:
        result.append(nested)
    return result



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Statement
» Yield