families = [[0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 
    3, 4, 5, 6]]

def f(i, j):
    print(i, j, families[i])
return families[i][j]
familiesNew = [[f(i, j) for j in families[i] if i != j] for i in range(len(
    families))]
Removing items from a nested list Python