0

How can I replace the following code written in python3 with normal sort() function?

    list = sorted(list, key=lambda x: x['result'], reverse=True)

NOTE: x['result'] is int

1
  • In the future, please explain what you mean by things like "the normal sort() function". It would be more helpful to say something like "the list.sort() method" for instance. Commented May 14, 2020 at 3:33

2 Answers 2

1

You can do exactly the same thing using list.sort(), i.e it takes in a "key" argument, all you have to do is to change the function call on position_list into a method call.

For example:

l.sort(key=lambda x: x["result"], reverse=True)

As far as the lambda is concerned, you can just substitute it with a real function instead, but I don't think anything else can be simpler than a lambda.

P.S: Have a look at the documentation: https://docs.python.org/3/library/stdtypes.html#list.sort

Sign up to request clarification or add additional context in comments.

1 Comment

Add examples instead of only documentations, I already read that before
0

Use: competition_list.sort(key=lambda x: x['result'], reverse=reversed_order). It sorts the list in place.

1 Comment

My question was not to use lambda but normal sort function

Your Answer

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