fork download
  1. def f(L):
  2. o=[];i=[];j=[]
  3. for x in L:[i,j][[]<x]+=[x]
  4. if i:o+=[i]
  5. if j:o+=f(sum(j,[]))
  6. return o
  7.  
  8. L = [1, 2, [3, [4, 5], 6, [7, [8], 9]]] # => [1, 2], [3, 6], [4, 5, 7, 9], [8]
  9. print f(L)
  10.  
  11. L = [3, 1, [12, [14, [18], 2], 1], [[4]], 5] #=> [[3, 1, 5], [12, 1], [14, 2, 4], [18]]
  12. print f(L)
  13.  
  14. L = [2, 1, [[5]], 6] #=> [[2, 1, 6], [5]]
  15. print f(L)
  16.  
  17. L = [[54, [43, 76, [[[-19]]]], 20], 12] #=> [[12], [54, 20], [43, 76], [-19]]
  18. print f(L)
  19.  
  20. L = [[[50]], [[50]]] #=> [[50, 50]]
  21. print f(L)
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
[[1, 2], [3, 6], [4, 5, 7, 9], [8]]
[[3, 1, 5], [12, 1], [14, 2, 4], [18]]
[[2, 1, 6], [5]]
[[12], [54, 20], [43, 76], [-19]]
[[50, 50]]