0

I have a nested list that looks like this

[[[0]],
[[2], [1]],
[[3]],
[[4]],
[[5]],
[[6]],
[[7]],
[[8]],
[[9, 10]],
[[11]],
[[13], [12]]]

I want to simplify it into not flatten everything

[[0], [2], [1],[3],[4],[5],[6],[7],[8],[9,10],[11],[13],[12]]

simplify the nested list.

Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51
ASking
  • 53
  • 7
  • Where do you get this input (maybe you need to fix it there?)? What have you tried so far? – buran Feb 03 '23 at 11:00
  • No I don't have access to the code that provides this output – ASking Feb 03 '23 at 11:00
  • Does this answer your question? [Flatten an irregular (arbitrarily nested) list of lists](https://stackoverflow.com/questions/2158395/flatten-an-irregular-arbitrarily-nested-list-of-lists) – FoxFil Feb 03 '23 at 11:03
  • No I don't want to flatten everything, I still need to keep [9,10] – ASking Feb 03 '23 at 11:05
  • 1
    Does this answer your question? [How do I make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-a-list-of-lists) The approved solution gave me your expected output. (the list comprehension one) – Franciska Feb 03 '23 at 11:05
  • Welcome to Stack Overflow. Looking at the specification, I see that you want to "make a flat list (of lists) out of a list of lists (of lists)". Hence that is the correct canonical duplicate here. – Karl Knechtel Feb 03 '23 at 11:07

2 Answers2

0

What you want to do is a variation of "flattening" of the arrays. here's an example solution for this:

nested_list = [[[0]],
[[2], [1]],
[[3]],
[[4]],
[[5]],
[[6]],
[[7]],
[[8]],
[[9, 10]],
[[11]],
[[13], [12]]]

simplified_list = []
for sub_list in nested_list:
    for item in sub_list:
        simplified_list.extend([item])

print(simplified_list)

it generates the output: [[0], [2], [1], [3], [4], [5], [6], [7], [8], [9, 10], [11], [13], [12]]

Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51
0

Use (my_list is the original):

flat = [item for sublist in my_list for item in sublist]
print(flat)
user19077881
  • 3,643
  • 2
  • 3
  • 14