How do I sort a list of dictionaries by a value of the dictionary?

masi

I have a list of dictionaries and want each item to be sorted by a specific value.

Take into consideration the list:

[{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]

When sorted by name, it should become:

[{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}]
Mario F

It may look cleaner using a key instead a cmp:

newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) 

or as J.F.Sebastian and others suggested,

from operator import itemgetter
newlist = sorted(list_to_be_sorted, key=itemgetter('name')) 

For completeness (as pointed out in comments by fitzgeraldsteele), add reverse=True to sort descending

newlist = sorted(l, key=itemgetter('name'), reverse=True)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I sort list of numerical value dictionaries?

From Dev

How do I sort a list in a dictionary

From Java

How do I sort a dictionary by value?

From Dev

How to sort a list of dictionaries of dictionaries by value

From Dev

How can I create a list of dictionaries as value of another dictionary?

From Dev

How do I add keys to a dictionary in a list of dictionaries?

From Dev

How can I sort (Custom Sort) list of Dictionary entry by value

From Dev

How do I sort a dictionary in decreasing order based on values of the list as its value?

From Dev

How do I sort a key:list dictionary by values in list?

From Dev

How to sort list by dictionary value?

From Dev

How to sort list by dictionary value?

From Dev

How do i sort a dictionary by value that has tuples as values

From Dev

Python: How do I get a list of all keys in a dictionary of dictionaries, at a given depth

From Java

How do you sort a dictionary by value?

From Dev

How do I shuffle a list of dictionaries?

From Dev

How do I create a list of dictionaries?

From Dev

How to modify Python dictionary value and create the list of dictionaries?

From Dev

Sort huge dictionary of dictionaries by nested value efficiently?

From Dev

How convert list of dictionaries to a dictionary of dictionaries in Python

From Dev

How convert list of dictionaries to a dictionary of dictionaries in Python

From Dev

How do I filter array of dictionaries by dictionary key

From Dev

How to sort a list of dictionaries in python?

From Dev

How do I filter DataFrame rows based on a key value pair from a list of dictionaries column field?

From Dev

How do I delete key/value pair in list of dictionaries pandas env

From Dev

How do I make a combobox display a list of dictionaries and act upon the selected value?

From Dev

How do I sort an array of Swift dictionaries by key

From Java

How to convert list of dictionaries into a dictionary of dictionaries by using inner key:value pair

From Dev

Accessing dictionary value from list of dictionaries

From Dev

find and update a value of a dictionary in list of dictionaries

Related Related

  1. 1

    How do I sort list of numerical value dictionaries?

  2. 2

    How do I sort a list in a dictionary

  3. 3

    How do I sort a dictionary by value?

  4. 4

    How to sort a list of dictionaries of dictionaries by value

  5. 5

    How can I create a list of dictionaries as value of another dictionary?

  6. 6

    How do I add keys to a dictionary in a list of dictionaries?

  7. 7

    How can I sort (Custom Sort) list of Dictionary entry by value

  8. 8

    How do I sort a dictionary in decreasing order based on values of the list as its value?

  9. 9

    How do I sort a key:list dictionary by values in list?

  10. 10

    How to sort list by dictionary value?

  11. 11

    How to sort list by dictionary value?

  12. 12

    How do i sort a dictionary by value that has tuples as values

  13. 13

    Python: How do I get a list of all keys in a dictionary of dictionaries, at a given depth

  14. 14

    How do you sort a dictionary by value?

  15. 15

    How do I shuffle a list of dictionaries?

  16. 16

    How do I create a list of dictionaries?

  17. 17

    How to modify Python dictionary value and create the list of dictionaries?

  18. 18

    Sort huge dictionary of dictionaries by nested value efficiently?

  19. 19

    How convert list of dictionaries to a dictionary of dictionaries in Python

  20. 20

    How convert list of dictionaries to a dictionary of dictionaries in Python

  21. 21

    How do I filter array of dictionaries by dictionary key

  22. 22

    How to sort a list of dictionaries in python?

  23. 23

    How do I filter DataFrame rows based on a key value pair from a list of dictionaries column field?

  24. 24

    How do I delete key/value pair in list of dictionaries pandas env

  25. 25

    How do I make a combobox display a list of dictionaries and act upon the selected value?

  26. 26

    How do I sort an array of Swift dictionaries by key

  27. 27

    How to convert list of dictionaries into a dictionary of dictionaries by using inner key:value pair

  28. 28

    Accessing dictionary value from list of dictionaries

  29. 29

    find and update a value of a dictionary in list of dictionaries

HotTag

Archive