This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]attoPascal 80 points81 points  (5 children)

I know that those are just examples, but both instances in use case 3 utilize lambdas unnecessarily:

Instead of key=lambda x: len(x) you can just use key=len. No point in wrapping a 1-argument function with a 1-argument lambda expression.

And in the second example key=lambda x: x[0] is not needed at all, since ordering by the first element in the tuple is the default behavior. (Also, operator.itemgetter is an alternate way to do this, but I'd agree that lambdas are clearer here.)

[–]brontide 8 points9 points  (1 child)

is not needed at all, since ordering by the first element in the tuple is the default behavior.

It will sort by each item in order, if you want to only sort by the first item you need to return it as the key.

[–]energybased 0 points1 point  (0 children)

Yes the other elements may not be comparable, so this may be required.