In this example we will show us how to access matrix rows or columns and print them out in Python.
Source Code
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print(matrix)
# Access 2nd row and print out
print(matrix[1])
# Access 2 column and print out
pp = [row[1] for row in matrix]
print(pp)
# Access 2 column and print out if a condition is met
ppx = [row[1] for row in matrix if row[1] % 2 == 0]
print(ppx)
Output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[4, 5, 6]
[2, 5, 8]
[2, 8]