Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to rank a list of dictionaries by the highest value of a certain key within the dictionary?

I have been trying for the past two days to sort this out and i can’t figure it out.

I have a list of dictionaries that needs to be printed out as a table, the employees need to be ranked by the number of properties sold, highest to lowest.

import tabulate

data_list = [
         {
          'Name': 'John Employee',
          'Employee ID': 12345,
          'Properties Sold': 5,
          'Commission': 2500,
          'Bonus to Commission': 0,
          'Total Commission': 2500
         },
         {
          'Name': 'Allie Employee',
          'Employee ID': 54321,
          'Properties Sold': 3,
          'Commission': 1500,
          'Bonus to Commission': 0,
          'Total Commission': 1500
         },
         {
          'Name': 'James Employee',
          'Employee ID': 23154,
          'Properties Sold': 7,
          'Commission': 3500,
          'Bonus to Commission': 525,
          'Total Commission': 4025
          }
         ]

header = data_list[0].keys()

rows = [x.values() for x in data_list]

print(tabulate.tabulate(rows, header))

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Name              Employee ID    Properties Sold    Commission    Bonus to Commission    Total Commission
--------------  -------------  -----------------  ------------  ---------------------  ------------------
John Employee           12345                  5          2500                      0                2500
Allie Employee          54321                  3          1500                      0                1500
James Employee          23154                  7          3500                    525                4025

Output needed:

Name              Employee ID    Properties Sold    Commission    Bonus to Commission    Total Commission
--------------  -------------  -----------------  ------------  ---------------------  ------------------
James Employee          23154                  7          3500                    525                4025
John Employee           12345                  5          2500                      0                2500
Allie Employee          54321                  3          1500                      0                1500

>Solution :

You can sort the list of dictionaries by the properties sold:

sorted_data_list = sorted(data_list, key=lambda x: x["Properties Sold"], reverse=True)

header = sorted_data_list[0].keys()

rows = [x.values() for x in sorted_data_list]

print(tabulate.tabulate(rows, header))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading