-2

What my list looks like:

my_list = [  [[1, 'a'],[1, 'b']],  [[2, 'a']],  [[3, 'a'],[3, 'b'],[3, 'c']]  ]

My ideal list:

my_list = [ [1, 'a'], [1, 'b'], [2, 'a'], [3, 'a'], [3, 'b'], [3, 'c'] ]

How can I achieve this?

j. doe
  • 163
  • 3
  • 11
  • 1
    Does this answer your question? [How to make a flat list out of list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) – costaparas Jan 06 '21 at 11:50
  • @MisterMiyagi i hope to keep the [int, char] structure, but I want to get rid of the extra nesting in my_list – j. doe Jan 06 '21 at 11:51

1 Answers1

1

just iterate with list comprehension and get the first one:

my_list = [l for li in my_list for l in li]

Output:

[ [1, 'a'], [1, 'b'], [2, 'a'], [3, 'a'], [3, 'b'], [3, 'c'] ]
adir abargil
  • 5,495
  • 3
  • 19
  • 29
  • This is not correct, check again the nesting in the question. – costaparas Jan 06 '21 at 11:46
  • 1
    @costaparas Did the recent edit fix your concerns? The result appears correct in my tests. – MisterMiyagi Jan 06 '21 at 11:50
  • Ah I see you removed the `map` version. List comprehension is right now. But `itertools` is much simpler for this anyway, as [here](https://stackoverflow.com/a/953097/14722562) in the dupe. – costaparas Jan 06 '21 at 11:51