itertools.product

Go Back home

itertools.product is a little known function that can be useful for iterating rows or columns of a matrix.

1 (0, 0)(0, 1)(0, 2)(0, 3)(0, 4)
2 (1, 0)(1, 1)(1, 2)(1, 3)(1, 4)
3 (2, 0)(2, 1)(2, 2)(2, 3)(2, 4)
4 (3, 0)(3, 1)(3, 2)(3, 3)(3, 4)

Say you want to select the two outer columns of this matrix:

1 from itertools import product as prod
2 for ind in prod(range(4), [0, 4]):
3     print(ind)