x.sort() sorts x but doesn't return anything. Fixed by running the sort command first and then returning x
.append is a method, so it changes not only the 'new_values' but also the 'values' list. Fixed by copying the original list using deepcopy.
In the trapz command x and y were at the wrong places. Fixed by using trapz(y, x=x) instead of trapz(x, y)
'y_new[i] =' is a method. It changes the original array, too. Fixed by creating an array of zeros with the length of y instead of using y directly fo y_new