def differences(nums):
    n = len(nums)
for i in range(n - 1):
    for j in range(i + 1, n):
        yield abs(nums[i] - nums[j])
for d in differences(idx):
    pass
Efficiently find differences in elements of a list