Need help with the puzzle. which breaks my brain.

m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 

there is a matrix and there is such a generator:

 [m[i][i] for i in range(len(m))] 

which displays the result - [1, 5, 9]

question. How to write a generator so that he would output the result - [3, 5, 7] is a generator, since it is not difficult to make a direct request to the matrix, and it is difficult to create a generator)

 m[0][2], m[1][1],m[2][0] 

    2 answers 2

     [m[i][len(m)-i-1] for i in range(len(m))] 
    • thank you so much) - Ruslan Rytchenko
     In [15]: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] In [16]: [matrix[x][-(x+1)] for x in range(len(matrix))] Out[16]: [3, 5, 7] 

    Such a range will give us the following indices:

     [(0, -1), (1, -2), (2, -3)] 
    • thanks for the decision - Ruslan Rytchenko