Question: The list contains x,y coordinates and looks like this,
`[[1,2], [2,3], [5,6]....]`
I know how to calculate the distance between two coordinates. I need to calculate the distance between[1,2] with all the other coordinates in the list, and then move on to [2,3] and do the same and so on.

What would be the best way to proceed with this problem?

My initial approach was to create two for loops:

```
for i in range (0, len(coordinateslist)):
    for j in range (0, len(coordinateslist)):
        distanceList.append(computeDist(coordinateslist[i),coordinateslist[j])

```

Answer: 
You need to define what pairs of coordinates you want to compare. See table below for what possible comparisons exists.

```
*   A  B  C  ...

A   AA AB AC
B   BA BB BC
C   CA CB CC
...          ...

```

Assuming valid comparisons are either (AB, AC, BC) Or (BA, CA, CB) but not both.

You need to change your loop slightly.

```py
from itertools import islice

for i, point in enumerate(coordinateslist):
    for other in islice(coordinateslist, i):
        distanceList.append(computeDist(point, other))

```
So a brute force solution might look like...
ex[x,y,z,l,m...]
calculate each paired distance exactly once
x:(points -x)
y:(points -x -y)
z:(points -x -y -z) 
etc...

```
def calculate_distances(points)
    tocalc = points
    answers = dict()
    for point in points:
        for dot in tocalc:
            if point!=dot: # distance to itself is always 0
                answers[sorted([point,dot])] = distance(point,dot)
        tocalc.pop(0) #no need to process this item again
     return answers

```

Then you can do things like `sum(answers.values())`, 'sorted(answers,key=lambda k: k.value)` , etc.

From the above it's clear that we don't actually need a second list to manage what to calculate, we just need two indices, so lets try and do it with minimal memory footprint:

```
def calculate_distances(points):
    currind=0
    tocalc_ind = 1
    # we also know the answer looks like a matrix with diagonal of zeros...
    answers = dict()
    for p_ind in range(len(points)):
        currind = p_ind
        if points[currind] not in answers:
            answers[points[currind]] = dict()
        for c_ind in range(tocalc_ind,len(points)): # implicitly skipping previous
            answers[points[currind]][points[c_ind]] = distance(points[currind],points[c_ind])
    return answers

```

Notice I changed the data format to help visualize the answer. I'm sure there are other optimizations, but this should run in O(n) time because the second nested loop of the usual O(n\*n) is decreased each turn.