I want to use a nested list comprehension to flatten a list. I use a nested list comprehension in the following way:
matrix = [['1','2','3',['4','5']],['A','B','C',['D','E']],['alpha','beta','charlie',['delta','echo']]]
[z for x in matrix for y in x for z in y]
This yields a result:
['1','2','3','4','5','A','B','C','D','E','a','l','p','h','a','b','e','t','a','c','h','a','r','l','i','e','delta','echo']
The nested list comprehension is a bit aggressive in also parsing the strings, which is undesired for my use case.
I am hoping to have an output that yields:
['1','2','3','4','5','A','B','C','D','E','alpha','beta','charlie','delta','echo']
I know this can be possibly achieved with iterator functions etc. I was wondering if there is a way to achieve this with List Comprehensions. Most of the solutions I saw were using Generators or other complex parsers.