Python - Extracting inner most lists

后端 未结 3 757
逝去的感伤
逝去的感伤 2021-01-01 13:35

Just started toying around with Python so please bear with me :)

Assume the following list which contains nested lists:

[[[[[1, 3, 4, 5]], [1, 3, 8]]         


        
3条回答
  •  清歌不尽
    2021-01-01 13:45

    More efficient than recursion:

    result = []
    while lst:
        l = lst.pop(0)
        if type(l[0]) == list:
            lst += [sublst for sublst in l if sublst] # skip empty lists []
        else:
            result.insert(0, l) 
    

提交回复
热议问题