Home » Python

How do I sort a dictionary by value in Python?

Sorting a dictionary by value: Here, we are going to learn how do I sort a dictionary by value in Python programming language?
Submitted by Sapna Deraje Radhakrishna, on October 25, 2019

The dictionary in a python is not a sorted collection. The reason for it being not sorted helps in the speed of access.

Consider an example dictionary,

    sample_dict = {'a':1, 'c':3, 'b':5, 'd':4}

In order to sort a dictionary by value, introduce another data structure such as an ordered dictionary.

Use the sorted function and operator module

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> sample_dict = {'a':1, 'c':3, 'b':5, 'd':4 }
>>> import operator
>>> sorted_dict = sorted(sample_dict.items(), key = operator.itemgetter(1))
>>> print(sorted_dict)
[('a', 1), ('c', 3), ('d', 4), ('b', 5)]
>>>

In the above example, we have used the operator module to sort the items in the dictionary by value. The output of the above function is of type list which is a list of tuples sorted by the second element in each tuple. Each tuple contains the key and value for each item found in the dictionary,

Use the sorted function and lambda

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> sample_dict = {'a':1, 'c':3, 'b':5, 'd':4 }
>>> sorted_dict = sorted(sample_dict.items(), key = lambda i:i[1])
>>> print(sorted_dict)
[('a', 1), ('c', 3), ('d', 4), ('b', 5)]
>>>

Use the sorted function and list comprehension

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> sample_dict = {'a':1, 'c':3, 'b':5, 'd':4 }
>>> sorted_dict = sorted((value, key) for (key,value) in sample_dict.items())
>>> print(sorted_dict)
[(1, 'a'), (3, 'c'), (4, 'd'), (5, 'b')]
>>>

Use the sorted function and return an Ordered dictionary along with lambda function

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> from collections import OrderedDict
>>> sample_dict = {'a':1, 'c':3, 'b':5, 'd':4 }
>>> sorted_ordered_dict = OrderedDict(sorted(sample_dict.items(),  key = lambda i:i[1]))
>>> print(sorted_ordered_dict)
OrderedDict([('a', 1), ('c', 3), ('d', 4), ('b', 5)])

In the above example the output if of type dictionary.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.