Menu Close

How to Sort the List Of Dictionaries By Value in Python

Sort the List Of Dictionaries By Value in Python

In this article, we are going to learn how to sort the list of dictionaries by value in Python with the help of examples. Sometimes you want to sort the list of dictionaries by specific key or value, Then you can follow all the methods which we will learn throughout this tutorial.

To sort the list of objects in Python we will see the various methods along with the examples so that you don’t have any confusion regarding How to Sort a List Of objects in Python.

Before going through this article, first of all, we will are going to know a little bit about Python Dictionary.

What is Python Dictionary?

Dictionary is one of the most important data types in Python. Python dictionary contains unordered collections of items, that’s why Python dictionary can not sort the keys or values.
Dictionaries in Python is made using key-value pair where value can be accessed by their corresponding key.

In this guide, we will demonstrate some custom ways as well pre-defined methods to sort the Python dictionary.

Example

This is an example of a simple Python dictionary named student.


student =  {
        "age": 21,
        "course": "BCA",
        "name": "Vishvajit",
        "address": "Noida",
        "marks": 500,

			}

Why do you need to sort the Python list of objects ?

As a developer when you are going to create the REST APIs, Then clients or someone who consumes your API requirement is that your response data should be sorted by specific value.
In That situation, you have to sort your Python list of objects just before the response returns.

Let’s go to sort the list of python dictionaries by value with the help of two methods.

Sort The List of Python Dictionaries

Here we will use two functions to sort the list of Python dictionaries.

  • sorted() function
  • itemgetter() function

Using sorted() Function

Here we are using Python built-in sorted() built-in function and also lambda function.
Basically, lambda is a single-line anonymous function.

Example:- Sort the list of objects in Python by value


students = [

    {
        "age": 21,
        "course": "BCA",
        "name": "Vishvajit",
        "address": "Noida",
        "marks": 500,

    },
     {
        "age": 28,
        "course": "BTech",
        "name": "Vinay",
        "address": "Lucknow",
        "marks": 510,

    },
    {
        "age": 30,
        "course": "MCA",
        "name": "Abhishek",
        "address": "Lucknow",
        "marks": 480,

    },
   
    {
        "age": 24,
        "course": "MTech",
        "name": "Shanaya",
        "address": "Noida",
        "marks": 570,

    }
]

# SORT BY AGE
result = sorted(students, key=lambda x: x["age"])
print(result)

Output


[{'address': 'Noida',
  'age': 21,
  'course': 'BCA',
  'marks': 500,
  'name': 'Vishvajit'},
 {'address': 'Noida',
  'age': 24,
  'course': 'MTech',
  'marks': 570,
  'name': 'Shanaya'},
 {'address': 'Lucknow',
  'age': 28,
  'course': 'BTech',
  'marks': 510,
  'name': 'Vinay'},
 {'address': 'Lucknow',
  'age': 30,
  'course': 'MCA',
  'marks': 480,
  'name': 'Abhishek'}]

Example 2: Sort the list of objects in Python by value

In this example, we are going to sort the students list of objects by age and name.


students = [

    {
        "age": 21,
        "course": "BCA",
        "name": "Vishvajit",
        "address": "Noida",
        "marks": 500,

    },
     {
        "age": 28,
        "course": "BTech",
        "name": "Vinay",
        "address": "Lucknow",
        "marks": 510,

    },
    {
        "age": 30,
        "course": "MCA",
        "name": "Abhishek",
        "address": "Lucknow",
        "marks": 480,

    },
   
    {
        "age": 24,
        "course": "MTech",
        "name": "Shanaya",
        "address": "Noida",
        "marks": 570,

    }
]

# SORT BY AGE
result = sorted(students, key=lambda x: (x["age"], x["name"]))
print(result)

Output


[{'address': 'Noida',
  'age': 21,
  'course': 'BCA',
  'marks': 500,
  'name': 'Vishvajit'},
 {'address': 'Noida',
  'age': 24,
  'course': 'MTech',
  'marks': 570,
  'name': 'Shanaya'},
 {'address': 'Lucknow',
  'age': 28,
  'course': 'BTech',
  'marks': 510,
  'name': 'Vinay'},
 {'address': 'Lucknow',
  'age': 30,
  'course': 'MCA',
  'marks': 480,
  'name': 'Abhishek'}]

Note:- Learn more about python sorted() function.

Using itemgetter() function

Here we are going to use itemgetter() function to sort the list of python dictionaries by value.

itemgetter():- itemgetter() function is defined inside the python operator module which is a built-in module so to use the itemgetter function you have to import it from the operator module.

Example


from pprint import pprint
from operator import itemgetter
students = [

    {
        "age": 21,
        "course": "BCA",
        "name": "Vishvajit",
        "address": "Noida",
        "marks": 500,

    },
    {
        "age": 30,
        "course": "MCA",
        "name": "Abhishek",
        "address": "Lucknow",
        "marks": 480,

    },
    {
        "age": 28,
        "course": "BTech",
        "name": "Vinay",
        "address": "Lucknow",
        "marks": 510,

    },
    {
        "age": 24,
        "course": "MTech",
        "name": "Shanaya",
        "address": "Noida",
        "marks": 570,

    }
]


result = sorted(students, key=itemgetter("name"))
pprint(result)

Output


[{'address': 'Lucknow',
  'age': 30,
  'course': 'MCA',
  'marks': 480,
  'name': 'Abhishek'},
 {'address': 'Noida',
  'age': 24,
  'course': 'MTech',
  'marks': 570,
  'name': 'Shanaya'},
 {'address': 'Lucknow',
  'age': 28,
  'course': 'BTech',
  'marks': 510,
  'name': 'Vinay'},
 {'address': 'Noida',
  'age': 21,
  'course': 'BCA',
  'marks': 500,
  'name': 'Vishvajit'}]

Example

Here we are going to sort students list of objects by name and address.



from pprint import pprint
from operator import itemgetter
students = [

    {
        "age": 21,
        "course": "BCA",
        "name": "Vishvajit",
        "address": "Noida",
        "marks": 500,

    },
    {
        "age": 30,
        "course": "MCA",
        "name": "Abhishek",
        "address": "Lucknow",
        "marks": 480,

    },
    {
        "age": 28,
        "course": "BTech",
        "name": "Vinay",
        "address": "Lucknow",
        "marks": 510,

    },
    {
        "age": 24,
        "course": "MTech",
        "name": "Shanaya",
        "address": "Noida",
        "marks": 570,

    }
]


result = sorted(students, key=itemgetter("name", "address"))
pprint(result)

Output


[{'address': 'Lucknow',
  'age': 30,
  'course': 'MCA',
  'marks': 480,
  'name': 'Abhishek'},
 {'address': 'Noida',
  'age': 24,
  'course': 'MTech',
  'marks': 570,
  'name': 'Shanaya'},
 {'address': 'Lucknow',
  'age': 28,
  'course': 'BTech',
  'marks': 510,
  'name': 'Vinay'},
 {'address': 'Noida',
  'age': 21,
  'course': 'BCA',
  'marks': 500,
  'name': 'Vishvajit'}]

Python itemgetter() function reference:- Click Here

Summary

So, In this article, we have seen all about How to sort the list of Dictionaries by value in Python. In this guide we explained a total of two ways to Sort a list of objects in Python, you can use any one of them by your choice.All the ways are legit. As a developer, you should have knowledge about how to sort the list of objects in Python

If you like this article, please share it and keep visiting for further Python interesting tutorials.

Thanks for your valuable time ….

How to Convert a Tuple to a DataFrame in Python
How to Convert String to DateTime in Python

Related Posts