Q:

Python program to sort dictionary key and values list

belongs to collection: Python Dictionary Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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]}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Dictionary Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Handle Missing Keys in Python Dictionary?... >>
<< Python program to insert an element at the beginni...