matrix with absolute difference between row and column index

Solution 1:

You can use a simple nested list comprehension:

n = 5
[[abs(i-j) for j in range(n)]
 for i in range(n)]

Output:

[[0, 1, 2, 3, 4],
 [1, 0, 1, 2, 3],
 [2, 1, 0, 1, 2],
 [3, 2, 1, 0, 1],
 [4, 3, 2, 1, 0]]

Or using numpy:

import numpy as np
n = 5
a = np.arange(n)
abs(a-a[:,None])