You have already found the error, and I will add only that the way you are trying to solve the problem is not very Python.
In python, we try not to iterate over elements by index. The for loop allows you to loop through items directly. So it turns out much clearer and reduces the likelihood of errors.
If, in some particular case, along with the element, its index is also needed (for example, as in your case, in order to use it to limit the nested loop), then you can use the enumerate function to get pairs (index, value).
cen_x = [770.5, 867.5, 1164.0, 312.0, 1256.0] kat_x = [] for i, x1 in enumerate(cen_x): for x2 in cen_x[i+1:]: k_x = abs(x1 - x2) kat_x.append(k_x) print(kat_x)
So read easier than in your case. In python, the readability of the code is very important, because it is necessary to read the code many times more than to write it.
But, in fact, your problem can be solved even easier.
In cycles you try to find all combinations of two elements. But for this, there is a ready-made function in python (there are a lot of ready-made functions in python in general).
from itertools import combinations cen_x = [770.5, 867.5, 1164.0, 312.0, 1256.0] pairs = combinations(cen_x, 2) kat_x = [abs(p[0] - p[1]) for p in pairs] print(kat_x)
I highly recommend to get acquainted with the itertools module and with other existing python libraries - both built-in and third-party.
There are not just a lot of them for python, but VERY much - for all occasions. And often you can not reinvent your own bike, but write in the search engine "python module% key_sword_your task%"
[abs(x - y) for x, y in zip(cen_x, cen_x[1:])]to do, but your answer from @Vlad from Moscow does something else. You can update the question (especially the title) and give a clearly expected result - jfs