Sayantan's Blog On Python Programming

PYTHON DICTIONARY SORTING OPERATIONS

PYTHON DICTIONARY SORTING OPERATIONS

INTRODUCTIONS

Python dictionaries can be sorted either in forward or backward direction by using below methods:

  • sorted() Function
  • Importing operator module
  • Python Lambda Function

sorted() Function

sorted() function is used to sort all the keys and values of a dictionary.

# PYTHON DICTIONARY SORTING OPERATIONS
# Example 1

dict_class_wise_prog = {'A':'Python', 'E':'XML', 'D':'PHP', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

print("\nList of keys of dict_class_wise_prog in sorted order ->", sorted(dict_class_wise_prog.keys()))
print("\nList of values of dict_class_wise_prog in sorted order ->", sorted(dict_class_wise_prog.values()))

PYTHON DICTIONARY SORTING OPERATIONS : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'E': 'XML', 'D': 'PHP', 'B': 'Java', 'C': 'C++'}

List of keys of dict_class_wise_prog in sorted order -> ['A', 'B', 'C', 'D', 'E']

List of values of dict_class_wise_prog in sorted order -> ['C++', 'Java', 'PHP', 'Python', 'XML']

In the above example we have used the sorted() function to sort the keys and values of a dictionary.

# PYTHON DICTIONARY SORTING OPERATIONS
# Example 2

dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

print("\nList of keys of dict_class_wise_count in sorted order ->", sorted(dict_class_wise_count.keys()))
print("\nList of values of dict_class_wise_count in sorted order ->", sorted(dict_class_wise_count.values()))

PYTHON DICTIONARY SORTING OPERATIONS : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}

List of keys of dict_class_wise_count in sorted order -> ['A', 'B', 'C', 'D', 'E']

List of values of dict_class_wise_count in sorted order -> [100, 200, 300, 400, 500]

Importing operator module

Python has provided operator module for sorting a dictionary either by keys or by values. The itemgetter() function of operator module will help in sorting the dictionary. It will take one parameter, 0 for sorting the dictionary based on keys and 1 for sorting the dictionary based on values.

# PYTHON DICTIONARY SORTING OPERATIONS
# Example 3

import operator

dict_class_wise_count = {'A':100, 'B':200, 'C':800, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

dict_class_wise_count_sort = dict(sorted(dict_class_wise_count.items(), key = operator.itemgetter(0)))
print("\nList of key value pair of dict_class_wise_count_sort in sorted order of keys ->", dict_class_wise_count_sort)

dict_class_wise_count_sort = dict(sorted(dict_class_wise_count.items(), key = operator.itemgetter(1)))
print("\nList of key value pair of dict_class_wise_count_sort in sorted order of values ->", dict_class_wise_count_sort)

PYTHON DICTIONARY SORTING OPERATIONS : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 800, 'D': 400, 'E': 500}

List of key value pair of dict_class_wise_count_sort in sorted order of keys -> {'A': 100, 'B': 200, 'C': 800, 'D': 400, 'E': 500}

List of key value pair of dict_class_wise_count_sort in sorted order of values -> {'A': 100, 'B': 200, 'D': 400, 'E': 500, 'C': 800}

In the above example, we have used the itemgetter() function of operator module to sort the dictionary dict_class_wise_count_sort. We have used the value 800 against the key “C“. When we have sorted the dictionary based on keys then 800 has come in 3rd position. But when we have sorted the dictionary based on values then it came at the last position.

# PYTHON DICTIONARY SORTING OPERATIONS
# Example 4

import operator

dict_class_wise_prog = {'A':'Python', 'E':'XML', 'D':'PHP', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

dict_class_wise_prog_sort = dict(sorted(dict_class_wise_prog.items(), key = operator.itemgetter(0)))
print("\nList of key value pair of dict_class_wise_count_sort in sorted order of keys ->", dict_class_wise_prog_sort)

dict_class_wise_prog_sort = dict(sorted(dict_class_wise_prog.items(), key = operator.itemgetter(1)))
print("\nList of key value pair of dict_class_wise_count_sort in sorted order of values ->", dict_class_wise_prog_sort)

PYTHON DICTIONARY SORTING OPERATIONS : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'E': 'XML', 'D': 'PHP', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_count_sort in sorted order of keys -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}

List of key value pair of dict_class_wise_count_sort in sorted order of values -> {'C': 'C++', 'B': 'Java', 'D': 'PHP', 'A': 'Python', 'E': 'XML'}

Python Lambda Function

# PYTHON DICTIONARY SORTING OPERATIONS
# Example 5

dict_class_wise_count = {'A':100, 'B':200, 'C':800, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

dict_class_wise_count_sort = dict(sorted(dict_class_wise_count.items(), key = lambda item:item[0]))
print("\nList of key value pair of dict_class_wise_count_sort in sorted order of keys ->", dict_class_wise_count_sort)

dict_class_wise_count_sort = dict(sorted(dict_class_wise_count.items(), key = lambda item:item[1]))
print("\nList of key value pair of dict_class_wise_count_sort in sorted order of values ->", dict_class_wise_count_sort)

PYTHON DICTIONARY SORTING OPERATIONS : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 800, 'D': 400, 'E': 500}

List of key value pair of dict_class_wise_count_sort in sorted order of keys -> {'A': 100, 'B': 200, 'C': 800, 'D': 400, 'E': 500}

List of key value pair of dict_class_wise_count_sort in sorted order of values -> {'A': 100, 'B': 200, 'D': 400, 'E': 500, 'C': 800}

In the above example, we have used the lambda function along with sorted function. We have used the value 800 against the key “C“. When we have sorted the dictionary based on keys then 800 has come in 3rd position. But when we have sorted the dictionary based on values then it came at the last position.

# PYTHON DICTIONARY SORTING OPERATIONS
# Example 6

dict_class_wise_prog = {'A':'Python', 'E':'XML', 'D':'PHP', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

dict_class_wise_prog_sort = dict(sorted(dict_class_wise_prog.items(), key = lambda item:item[0]))
print("\nList of key value pair of dict_class_wise_prog_sort in sorted order of keys ->", dict_class_wise_prog_sort)

dict_class_wise_prog_sort = dict(sorted(dict_class_wise_prog.items(), key = lambda item:item[1]))
print("\nList of key value pair of dict_class_wise_prog_sort in sorted order of values ->", dict_class_wise_prog_sort)

PYTHON DICTIONARY SORTING OPERATIONS : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'E': 'XML', 'D': 'PHP', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog_sort in sorted order of keys -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}

List of key value pair of dict_class_wise_prog_sort in sorted order of values -> {'C': 'C++', 'B': 'Java', 'D': 'PHP', 'A': 'Python', 'E': 'XML'}

In the above example also the dictionary is sorted based on values and keys by using lambda function and sorted() function.

PYTHON DICTIONARY SORTING OPERATIONS : Output
PYTHON DICTIONARY SORTING OPERATIONS : Output

RELATED TOPICS:

Leave a Comment

Your email address will not be published. Required fields are marked *