Sunday, March 26, 2017

Flatten a list

# Paste your function here
def flatten(aList):
    '''
    aList: a list
    Returns a copy of aList, which is a flattened version of aList
    '''
    flattened_list = []
    for item in aList:
        if type(item) == type([]):
            flattened_list.extend(flatten(item))
        else:
            flattened_list.append(item)
    return flattened_list

No comments:

Post a Comment