Hello Nic, Python lists has a very powerfull buid-in "sort". edges = list(G.edges()) edges.sort(key = lambda u, v: (ddeg(u), ddeg(v))) for u, v in edges: print u,v, # Note the training comma to avoid newline print HTH, Miki http://pythonwise.blogspot.com