fork download
  1. def flatten(ls):
  2. for item in ls:
  3. if isinstance(item, list):
  4. for subitem in flatten(item):
  5. yield subitem
  6. else:
  7. yield item
  8.  
  9. xs = [
  10. [
  11. [4,5,6],
  12. [4,1,3]
  13. ],
  14. [
  15. [0,0,1]
  16. ],
  17. [
  18. [2,1],
  19. [6,8],
  20. [
  21. [9,1,3],
  22. [7,0,2]
  23. ]
  24. ],
  25. 4,
  26. 6,
  27. [7,8,9],
  28. ]
  29.  
  30. print(xs)
  31.  
  32. ys = list(flatten(xs))
  33.  
  34. print(ys)
Success #stdin #stdout 0.05s 9452KB
stdin
Standard input is empty
stdout
[[[4, 5, 6], [4, 1, 3]], [[0, 0, 1]], [[2, 1], [6, 8], [[9, 1, 3], [7, 0, 2]]], 4, 6, [7, 8, 9]]
[4, 5, 6, 4, 1, 3, 0, 0, 1, 2, 1, 6, 8, 9, 1, 3, 7, 0, 2, 4, 6, 7, 8, 9]