Subtracting the ith value from 2 different arrays

Solution 1:

this would be:

actual_y = [10,20,30,40,50,60,70,80,90]
predicted_y = [1,2,3,4,5,6,7,8,9]
difference = [i-j for i,j in zip(actual_y,predicted_y)]

print(abs(difference))

Output:

[9, 18, 27, 36, 45, 54, 63, 72, 81]

Solution 2:

alternatively you could use numpy :

import numpy as np
actual_y = np.array([10,20,30,40,50,60,70,80,90])
predicted_y = np.array([1,2,3,4,5,6,7,8,9])
print(actual_y - predicted_y)

output :

>>> [ 9 18 27 36 45 54 63 72 81]

Solution 3:

The previous solution are fine, but if you still want to use the for loop, it may look like this.

actual_y = [10,20,30,40,50,60,70,80,90]
predicted_y = [1,2,3,4,5,6,7,8,9]


D = []
for i in range(0,len(actual_y)):
   B = actual_y[i] - predicted_y[i]
   D.append(B)
print(D)


[9, 18, 27, 36, 45, 54, 63, 72, 81]