I am trying to use lambda do to some sorting on a list. What I wanted to do is sort the coordinates based on their manhattan distance from an inital poisition. I know I have most of the syntax down but it seems like I am missing something small, Thanks!
while (len(queue) > 0):
queue.sort(queue, lambda x: util.manhattanDistance(curr,x))
while len(queue) > 0:. But, since 0 is false and any other integer is not, this is identical towhile len(queue):. And, since an empty sequence is false and any other sequence is not, this is identical towhile queue:. And that's how you should write it—it's more idiomatic, easier to read, shorter, and possibly even more efficient.queueas the first argument toqueue.sort. Whatever typequeueis, that's not right. (Oh, and don't call a variablequeue; that's the name of a standard library module.)sortis not going to change the length of the object, so this is just going to loop forever, re-sorting an already-sorted list over and over.