Question: I am trying to create a list of integers and then scan it in order to find the minimum absolute value of the substractions of the elements of the list. I have created the list, but there is problem in the code which finds the minimum absolute value, as the result it shows is not correct. I think it is probably in the possitions of the elements of the list during the loops. Can you help me find it?

For example, when I create a list `Α = [2, 7, 5, 9, 3, 1, 2]`, the result of `min` should be 0, but it is 1.

Here is my code:

```
min=1000
for i in range (1, N-1):
    for j in range (i+1, N):
        if (abs (A [i-1] - A [j-1])<min):
            min = abs (A [i-1] - A [j-1])
print ("%d" %min)

```

Answer: 
You can do it like this:

```
A = [2, 7, 5, 9, 3, 1, 2]

temp = sorted(A)
min_diff = min([abs(i - j) for i, j in zip(temp [:-1], temp [1:])])

print(min_diff)  # -> 0

```

Sorting makes sure that the element pair `(i, j)` which produce the overall smallest difference would be a pair of consecutive elements. That makes the 
number of checks you have to perform much less than the brute force approach of **all** possible combinations.

---

Something a bit more clever that short-circuits:

```
A = [2, 7, 5, 9, 3, 1, 2]

def find_min_diff(my_list):
    if len(set(my_list)) != len(my_list):  # See note 1
        return 0
    else:
        temp = sorted(my_list)
        my_min = float('inf')
        for i, j in zip(temp [:-1], temp [1:]):
            diff = abs(i - j)
            if diff < my_min:
                my_min = diff
        return my_min

print(find_min_diff(A))  # -> 0

```

Notes:
======

1: Converting to set removes the duplicates so if the corresponding set has less elements than the original list it means that there is at least one duplicate value. But that necessarily means that the min absolute difference is 0 and we do not have to look any further. 

I would be willing to bet that this is the fastest approach for all lists that would return 0.
This is a brute-force solution:

```
from itertools import combinations

A = [2, 7, 5, 9, 3, 1, 2]

min(abs(i-j) for i, j in combinations(A, 2))  # 0

```