Computer Science, asked by pankajkumar7812, 8 months ago

Write a Python script to sort (ascending and descending) a dictionary by key?​

Answers

Answered by queensp73
2

Answer:

import operator

d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

print('Original dictionary : ',d)

sorted_d = sorted(d.items(), key=operator.itemgetter(1))

print('Dictionary in ascending order by value : ',sorted_d)

sorted_d = dict( sorted(d.items(), key=operator.itemgetter(1),reverse=True))

print('Dictionary in descending order by value : ',sorted_d)

Sample Output:

Original dictionary :  {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

Dictionary in ascending order by value :  [(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]

Dictionary in descending order by value :  {3: 4, 4: 3, 1: 2, 2: 1, 0: 0}

Explanation:

hope it helped !

plz mark as brainliest !

>>>>THANK U<<<<

#keep smiling :)

Similar questions