Python - List Comprehensions and Matrixes

Introduction

List comprehensions can process Matrixes.

Demo

M = [[1, 2, 3], 
     [4, 5, 6], # from   w  w  w .  j a  va 2s. co m
     [7, 8, 9]] 


print( M[1] )              # Row 2 
print( M[1][2] )           # Row 2, item 3 

d=[row[1] for row in M]                          # Column 2 
print( d )

d=[M[row][1] for row in (0, 1, 2)]               # Using offsets 
print( d )

Result

Related Topic