I am trying to create a program that calculates the center of gravity of the contour from the contour data and calculates the angle θ between the center of gravity and the coordinates of the contour.
There are the coordinates x_CoG [i] and y_CoG [i] of the center of gravity of the contour contours1 [i], which are given in advance, and I am trying to find θ by the following program using them.
In order to obtain θ, the x-direction distance x_d and the y-direction distance y_d from the center of gravity to the contour coordinates are obtained, and these are included in the function for obtaining the arc tangent. The data of x_d is stored in x_d_list, and the data of arctangent is stored in theta, and the numbers of those elements should match, but they do not match and the result is as follows.
I think I made a mistake in the for statement processing, but I don't know what to improve.

Result of print (len (theta))

Result of print (len (x_d_list))

Corresponding source code
x_d_list = []
y_d_list = []
theta = []
for i in range (0, len (contours1)):
    buf_np = contours1 [i] .flatten () # Since it is a multiple array of numpy, expand it once.
    for j, elem in enumerate (buf_np): #j is a loop that outputs θ for the coordinates in contours [i]
        if j% 2 == 0: Divide into # x, y
            x_d = elem --x_CoG1 [i] # x_d = x-direction distance from the center of gravity to the contour coordinates
            x_d_list.append (x_d)
        else: else:
            y_d = elem --y_CoG1 [i]
            y_d_list.append (y_d)
        theta.append (math.atan2 (y_d, x_d)) #thetaθ = atan2 (y, x)
print (len (theta))
print (len (x_d_list))
What I tried

Please describe here what you have tried for the problem.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.