Double for iteration generator

Hello everyone !
I’m a total beginner in python, and i’m still familiarizing myself with the python syntax, especially with the generation in list, especially when I have to use several of them.
Here’s where i’m stuck :
I’m creating a function that treats matrix that are fed to the function this way:

1 2 3
4 5 6
7 8 9

as you can see, the matrix is given with embedded newlines and the numbers separeted by spaces. To have a simpler time, I tried to seperate the matric in rows, and for this I did the following :

def myfunc(matrix):
        formated_matrix = [b for b in matrix.split("\n")]
        formated_matrix2 = [a.split(" ") for a in self.test]

given the result [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']] But I was wondering if it was possible to do it in one line, something like
[a for b in matrix_string.split("\n") for a in b.split(" ")]
but it gives the result ['1', '2', '3', '4', '5', '6', '7', '8', '9'] and I cannot figure out how to get the same result as previously stated.

Thanks a lot fot your help !

[ cell.split(" ") for cell in [row for row in m.split("\n")] ]

but in your case it also creates two empty elements that you’ll have to remove, eg.:
[ cell.split(" ") for cell in [row for row in m.split("\n")] ][1:-1]

Yup, the empty spaces are cleared in the beginning of my function so it shouldn’t be a problem. Can I enquire wether you use cell arbitrarily as a variable or is it a built-in function in python ? I’ve checked cell object but it doesn’t look related.

Other than that i should do the trick. Thanks a lot !

It’s just a clearer name and not even used in any function or keyword context.