title: Find the Shortest Distance between sets of GPS Coordinates - Python
body: <p>For example this array of GPS Coordinates:</p>
  
  <pre><code>GPSS = [{"Lat":40.641099,"Lon": -73.917094},{"Lat":40.60442,"Lon": -74.054873},{"Lat":40.779582,"Lon": -73.920213},{"Lat":40.651616,"Lon": -73.89097},{"Lat":40.755183,"Lon": -73.846248}]
  </code></pre>
  
  <p>I have already calculated the Distances below for each possible combination:</p>
  
  <pre><code>Distances = [{'GPSS': [0, 1], 'Distance': 12.34895151892164}, {'GPSS': [0, 2], 'Distance': 15.380561959360797}, {'GPSS': [0, 3], 'Distance': 2.499303143635897}, {'GPSS': [0, 4], 'Distance': 14.012560598709298}, {'GPSS': [1, 2], 'Distance': 22.53687775052488}, {'GPSS': [1, 3], 'Distance': 14.824576927209662}, {'GPSS': [1, 4], 'Distance': 24.318038568441654}, {'GPSS': [2, 3], 'Distance': 14.423642658224264}, {'GPSS': [2, 4], 'Distance': 6.807346029310139}, {'GPSS': [3, 4], 'Distance': 12.106031672624894}]
  </code></pre>
  
  <p>0,1 = referring to 40.641099,-73.917094 and 40.60442,-74.054873</p>
  
  <p>1,4 = 40.641099,-73.917094 and 40.755183,-73.846248</p>
  
  <p>I would now like to find out the shortest Distance (route) to visit each set of coordinates, so it's most likely not going to be point 0 to 1 to 2 to 3 to 4.
  But something like 1 to 3 to 4 to 2 to 0.</p>
  
  <p><strong>How would I accomplish something like this?</strong></p>
  
  <p>This is as far as I got:</p>
  
  <pre><code>for index, d in enumerate(Distances):
      print(d['GPSS'])
      Total = d['Distance']
      Start = d['GPSS'][1] #[0]
      CheckPoints = []
      CheckPoints.append(d['GPSS'][0])
      CheckPoints.append(d['GPSS'][1])
      for index2, d2 in enumerate(Distances):
          if index != index2:
              if Start == d2['GPSS'][0]: #0-1, 1-2, 2-3
                  Total += d2['Distance']
                  Start += 1
                  if d2['GPSS'][0] not in CheckPoints:
                      CheckPoints.append(d2['GPSS'][0])
                  if d2['GPSS'][1] not in CheckPoints:
                      CheckPoints.append(d2['GPSS'][1])
                  #print(CheckPoints)
                  print("+"+str(d2['Distance'])+" = "+str(Total)+" | "+str(Start)+" - "+str(d2['GPSS']))
  
      if len(CheckPoints) &lt;= len(GPSS)-1: #GPPS - is from above
          for x in range(len(GPSS)-1):
              if x not in CheckPoints:
                  for d3 in Distances:
                      if d3['GPSS'][0] == x and d3['GPSS'][1] == CheckPoints[-1]:
                          print("HERE")
                          print(d3)
                          Total += d3['Distance']
                          break
  
      print(Total)
  </code></pre>
  
  <p>Any help would be much appreciated.
  Thanks</p>