Python program to sort dictionary key and values list

Here, we are going to learn how to sort a dictionary keys and the values list which is used as values in Python?
Submitted by Shivang Yadav, on May 27, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Dictionary is a collection in python which is used to store data as Key:value pair.

Example:

dict = { "python" : 1, "C++" : 3, "javaScript" : 2 } 

Sort a Dictionary key and values list

We are given a dictionary with key-value pairs in an unsorted manner where values are an unordered list. We need to create a program that will sort the keys of the dictionary i.e. make it an ordered one and also sort the value list of the dictionary. 

Input:
dict = {'john' : [70, 82, 55], 'ram' : [92, 99, 98], 'jane' : [65, 34, 76]}

Output: 
dict = { 'jane' : [34, 34, 76], 'john' : [55, 70, 82], 'ram' : [92, 98, 99]}

In the Python programming language, we can sort any collection using the sorted method. We need to first sort the dictionary and then sort the value list for each key in the dictionary. A simple solution for this is using the sorted function and then for each of the key's values again use the sorted function. And store these sorted values in a new dictionary.

Program 1: Sort a dictionary key and values list

# Python program to sort dictionary key and values list

# Creating a list with list as values
myDict = {'john' : [70, 82, 55], 'ram' : [92, 99, 98], 'jane' : [65, 34, 76]}
print("Initially the dictionary is " + str(myDict))

# Sorting dictionary
sortedDict = dict()
for key in sorted(myDict):
	sortedDict[key] = sorted(myDict[key])

# Printing sorted dictionary 
print("Dictionary after sort of key and list value : " + str(sortedDict))

Output:

Initially the dictionary is {'jane': [65, 34, 76], 'john': [70, 82, 55], 'ram': [92, 99, 98]}
Dictionary after sort of key and list value : {'jane': [34, 65, 76], 'john': [55, 70, 82], 'ram': [92, 98, 99]}

Python also provides the function to perform the above task in a single line of code using dictionary comprehension which is a single line of code which replaces the logic for sorting with a single line of code which will perform the same task. Here, is the line of code to be added to our program,

{key : sorted(myDict[key] ) for key in sorted(myDict)} 

If you observe it closely it's the same code but in a single line and a neat way (these are good coding practices you should learn).

Program 2: Sort a dictionary key and values list

# Python program to sort dictionary key and values list

# Creating a list with list as values
myDict = {'john' : [70, 82, 55], 'ram' : [92, 99, 98], 'jane' : [65, 34, 76]}
print("Initially the dictionary is " + str(myDict))

# Sorting dictionary
sortedDict = {key : sorted(myDict[key] ) for key in sorted(myDict)} 

# Printing sorted dictionary 
print("Dictionary after sort of key and list value : " + str(sortedDict))

Output:

Initially the dictionary is {'jane': [65, 34, 76], 'john': [70, 82, 55], 'ram': [92, 99, 98]}
Dictionary after sort of key and list value : {'jane': [34, 65, 76], 'john': [55, 70, 82], 'ram': [92, 98, 99]}

Python Dictionary Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.