Displaying nodes sorted by distance to a given node in Python
<p>I have a number of nodes, each has an x-axis and y-axis values which indicate a distance from each other. For example, </p>

<pre><code>nodes = [
{'name': 'node1', 'x': 2, 'y': 4, 'friend': True},
{'name': 'node2', 'x': -3, 'y': 2, 'friend': True},
{'name': 'node3', 'x': 5, 'y': 0, 'friend': False},
{'name': 'node4', 'x': -6, 'y': 1, 'friend': False},
{'name': 'node5', 'x': 0, 'y': 3, 'friend': True} 
]
</code></pre>

<p>I need to create a function that returns only nodes who are friends sorted by a distance to a given node (say <code>main_node = nodes[0]</code>) based on the X and Y values.</p>

<p>I would appreciate your help.</p>

<p>The sort function takes a key parameter:</p>

<pre><code>main_node = nodes[0]
nodes.sort(key=lambda node: math.hypot(node["x"] - main_node["x"], node["y"] - main_node["y"]))
</code></pre>

python function sorting nodes