fork download
  1. def flatten(l):
  2. for el in l:
  3. if not el or not isinstance(el[0], list):
  4. yield el
  5. else:
  6. yield from flatten(el)
  7.  
  8. def nested_to_listoflist(l):
  9. return [el for el in flatten(l)]
  10. # or return list(flatten(l))
  11.  
  12.  
  13. nested_list = [[[1, 2, 3], [[5, 6, 7], [8, 9, 10, 11, 23]]], [4], [[12, 13, 14], [[15, 16], [[17, 18], [19, 20]]]], [21, 22, 25, 26]]
  14. unnested_list = nested_to_listoflist(nested_list)
  15. print(unnested_list)
Success #stdin #stdout 0.03s 9108KB
stdin
Standard input is empty
stdout
[[1, 2, 3], [5, 6, 7], [8, 9, 10, 11, 23], [4], [12, 13, 14], [15, 16], [17, 18], [19, 20], [21, 22, 25, 26]]