Skip to content
Advertisement

Sorting list by the inner elements in Python?

How that list can be sorted by the inner list elements?

JavaScript

I need a list at the final like that;

JavaScript

It will check the first elements of the inner lists, if there are more than one “True”, it will check and sort the second elements. If there are the same numbers, than it will check the third elements.

Advertisement

Answer

Use the key parameter of sorted:

JavaScript

Output

JavaScript

The idea of:

JavaScript

is to negate the first argument to put the inner list with True first, then use the normal int comparison for the rest of the elements.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement