# Solution Exercise 112: code python to sort list values of given dictionary

Sep 24, 2020

#### Exercise 112

Given a dictionary d whose key values are lists. Write a Python program that transforms the dictionary d by sorting the lists. Example for the dictionary:

``d = {'a1': [21, 17, 22, 3], 'a2': [11, 15, 8, 13], 'a3': [7, 13, 2, 11], 'a4': [22,14,7,9]}``

The program should return the dictionary:

``d = {'a1': [3, 17, 21, 22], 'a2': [8, 11, 13, 15], 'a3': [2, 7, 11, 13], 'a4': [7, 9, 14, 22]}``

#### Solution

``d = {'a1': [21, 17, 22, 3], 'a2': [11, 15, 8, 13], 'a3': [7, 13, 2, 11], 'a4': [22,14,7,9]}# getting the dictionary values and keys by using the items() methodefor key , value in d.items():    # sorting the list value    value.sort()    # updating the dictionary d    d[key] = value# display resultprint("The new dictionary is d = " , d)# The output is : The new dictionary is d =  {'a1': [3, 17, 21, 22], 'a2': [8, 11, 13, 15], 'a3': [2, 7, 11, 13], 'a4': [7, 9, 14, 22]}``

Younes Derfoufi
my-courses.net

#### By My- Courses

##### One thought on “Solution Exercise 112: code python to sort list values of given dictionary”
1. I agree with your point of view, your article has given me a lot of help and benefited me a lot. Thanks. Hope you continue to write such excellent articles.