How to flatten a hetrogenous list of list into a single list in python?

前端 未结 11 1712
暖寄归人
暖寄归人 2020-12-01 22:59

I have a list of objects where objects can be lists or scalars. I want an flattened list with only scalars. Eg:

L = [35,53,[525,6743],64,63,[743,754,757]]
ou         


        
11条回答
  •  暖寄归人
    2020-12-01 23:28

    outputList = []
    for e in l:
        if type(e) == list:
            outputList += e
        else:
            outputList.append(e)
    
    >>> outputList
    [35, 53, 525, 6743, 64, 63, 743, 754, 757]
    

提交回复
热议问题