Look at the documentation (or built-in help, or how-to. The signature for list.sort is:
s.sort([cmp[, key[, reverse]]]) sort the items of s in place (7)(8)(9)(10)
The notes explain, among other things, what each of those parameters mean. In particular, the first parameter, cmp:
specifies a custom comparison function of two arguments (list items)
You're passing toVisit as the first argument. So, toVisit will be used as a comparison function. You can't possibly have thought that was what you wanted. So, I'm guessing you probably don't get the basics of objects and dot notation, and you need to read an appropriate tutorial. (Sorry, I don't have one to recommend. I checked the official Python tutorial, but it seems to assume that dot notation, and what a method is, and so on are all just obvious and need no explanation…)
Here's the right version:
toVisit.sort(key = lambda x: util.manhattanDistance(curr,x))
Meanwhile, there doesn't seem to be any problem with the lambda function itself, but you seem to be convinced, in two questions in a row, that the lambda is the part you're having trouble with. If you ever have trouble with a lambda, the easiest thing to do is transform it into a normal named function, which is trivial to do.
Where you have this:
lambda ARGS: EXPR
Do this a line above it:
def good_name_for_what_expr_does(ARGS):
return EXPR
Then, replace the lambda ARGS: EXPR with good_name_for_what_expr_does.
So, this is equivalent to your original code:
def manhattanDistanceFromCurr(x):
return util.manhattanDistance(curr, x)
toVisit.sort(toVisit, key = manhattanDistanceFromCurr)
Can you see the problem there any more easily than in the lambda version?
toVisitis a very unusual type, you should not be passing it as the first argument totoVisit.sort. It doesn't need a copy of itself. And I already explained that in a comment to your last question.something.sort()We do not expect to see code like you have:something.sort(something)In Python, method functions on an object do not need a reference to the object passed in to the call. The only place I can think of where I would expect to see something like that is plain C code.