My Coding > Programming language > Python > Python FAQ > Python: how to sort list of objects by few parameters

Python: how to sort list of objects by few parameters

This is a very common task, to create procedure, which will sort the list of objects by it’s parameters. But if the first parameters of the object are identical, it will sort by second parameter, and if they are identical, then the comparison will be performed by third parameter. How to make this sorting procedure?

It is very easy to do with key parameter and lambda() function. But before, let’s see, how lists are sorted.

Sorting the list of lists

By default, in Python the lists are sorted by it’s elements with few following rules:

  • Lists are sorted by it’s elements
  • If elements are equal, then the lists are sorted by next elements
  • If one list have less elements but all first elements are equal – then this list considered as smaller

Let’s check it on this simple example

lst = [[3, 5], [3, 5, -1], [3, 5, 0], [3], [4], [-2, 3]]
s_lst = sorted(lst)
print(s_lst) # [[-2, 3], [3], [3, 5], [3, 5, -1], [3, 5, 0], [4]]

You can see that:

  • -2 < 3.
  • Then [3] has the same first element as [3, 5] but it is shorter.
  • The same [3, 5] is shorter than [3, 5, -1]
  • In [3, 5, -1] first different element (-1) is smaller than in the next list (0)
  • Again first element (4) is bigger than previous list have (3)

Sorting of any objects by few parameters

After studying how lists are sorted, we can convert out task of sorting objects by few parameters into a task of sorting lists with these parameters.

lambda() function

This is a short reminder about lambda() function: lambda L: some_function(L). In this example lambda function take one parameter L and return some results calculated as described in some_function(L)> - this is not necessary function, it can be anything.

General expression for sorting by few parameters

We have list lst with objects, which we would like to sort by its parameters, which can be calculated by following function f1(), f2(), f3(), then we can do it this way:

sorted_lst = sorted(lst, key=lambda L: (f1(L), f2(L), f3(L))

As simple as that! So, we sort the list of parameters, calculated on demand for every element of our list.

Example of sorting list by some parameters

Let’s sort lists in our list by their sum, length and maximal value.

lst = [[-40, 40], [0, 0], [30, -30], [-10, 20], [0]]

sorted_lst = sorted(lst, key=lambda L: (sum(L), len(L), max(L)))
# [[0], [0, 0], [30, -30], [-40, 40], [-10, 20]]

So, what was happen here? It is very simple to explain:

  • Sorting by sum – all objects are identical, except [-10, 20] – this is a biggest sum and it is go on right
  • All other elements are compared by they length and [0] is shortest – move to the left
  • Outstanding lists [0, 0], [30, -30], [-40, 40] are sorted by their maximal element

Published: 2021-11-23 15:09:11
Updated: 2021-11-25 04:46:01

Last 10 artitles

9 popular artitles

© 2020 -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.