How do I sort a dictionary by value?



How do I sort a dictionary by value?..

Answer / Digvijay Pratap Singh

To sort a Python dictionary by its values, you can convert the dictionary to list of tuples (key, value), and then sort it using sorted() function with reverse=True if you want the highest values first. Here's an example:n```pythonndict = {'a': 12, 'b': 34, 'c': 50, 'd': 17}nsorted_dict = dict(sorted(dict.items(), key=lambda item: item[1], reverse=True))nprint(sorted_dict)```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Python Interview Questions

In python,how to read a file line-by-line into a list?

1 Answers  


Is python good for oop?

1 Answers  


Why python is so popular?

1 Answers  


Which is better for future java or python?

1 Answers  


Under what circumstances would von use a while statement rather than for?

1 Answers  


Why Lambda is used in Python?

3 Answers  


What is hashmap in python?

1 Answers  


What is __ new __ in python?

1 Answers  


Is python full stack?

1 Answers  


Explain the //, %, and ** operators in python?

1 Answers  


What is for-else and while-else in python?

1 Answers  


how do I pass a variable by reference?

1 Answers  


Categories