2

I simply want to sort a list... and I have a 2 parameter lambda Here is my simple code:

I.sort(key = lambda x, y: x.finish - y.finish)

And the compiler return this error

builtins.TypeError: <lambda>() missing 1 required positional argument: 'y'
4
  • 2
    You can't use a two parameter lambda as the key for sort. It passes in each item one at a time. Commented Oct 12, 2016 at 14:46
  • 1
    You are confusing the cmp and key arguments; cmp is entirely gone from Python 3. For key, just use key=lambda x: x.finish Commented Oct 12, 2016 at 14:47
  • What does your list look like? Commented Oct 12, 2016 at 14:51
  • it is a list of time interval... def__init__(self, title, start, finish): Commented Oct 12, 2016 at 15:15

1 Answer 1

2

You are trying to use key function as a cmp function (removed in Python 3.x), but don't you mean to simply sort by the "finish" attribute:

I.sort(key=lambda x: x.finish)

Or, with the "attrgetter":

from operator import attrgetter

I.sort(key=attrgetter("finish"))
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the quick... What I want to do, is to sort by the interval of two finish

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.