I just investigated some more myself and found this solution making use of the walrus operator:
import numpy as np
a = np.arange(10)
dim_a, dim_total = 2, 4
(shape := [1] * dim_total)[dim_a] = -1
np.reshape(a, shape)
I like that it's very compact, but the :=
is still not very commonly used.