Python - Extracting inner most lists

后端 未结 3 959
失恋的感觉
失恋的感觉 2021-01-01 13:11

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:58

    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) 
    

提交回复
热议问题