Cleaner method for finding the shortest distance between points in a python list?
<p>I have a list of tuples and an individual point in python e.g. [(1,2) , (2,5), (6,7), (9,3)] and (2,1) , and I want to figure out the fastest path possible created by all combinations of the individual point to the list of points.(Basically I want to find the most efficient way to get to all of the points starting from (2,1)). I have a manhattanDistance function that can take it 2 points and output the distance. However, my algorithm is giving me inconsistent answers (The heuristic is off for some reason)</p>

<p>What would be the correct way to accomplish this?</p>

<p>Here is my previous algorithm:</p>

<pre><code>def bestPath(currentPoint,goalList):
    sum = 0
    bestList = []
    while len(goallist) &gt; 0:
        for point in list:
            bestList.append((manhattanD(point,currentPoint),point))
        bestTup = min(bestList)
        bestList = []
        dist = bestTup[0]
        newP = bestTup[1]
        currentPoint = newP
        sum += dist
return sum
</code></pre>

<p>If this is anything like the traveling salesman problem, then you want to check out the <a href="https://networkx.github.io/" rel="nofollow">NetworkX</a> python module.  </p>

python for-loop while-loop distance heuristics