Grow one tree by absorbing the cheapest frontier edge.
def prim(graph, start):
visited = {start}edges = [(w, start, v) for v, w in graph[start]]
heapify(edges)
while edges:
w, u, v = heappop(edges) # Settle edge
if v not in visited:
visited.add(v) # Absorb node
for nv, nw in graph[v]:
heappush(edges, (nw, v, nv))
Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.