{"id":3446,"date":"2021-04-23T13:25:35","date_gmt":"2021-04-23T07:55:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=3446"},"modified":"2021-11-22T18:44:54","modified_gmt":"2021-11-22T13:14:54","slug":"python-how-to-sort-a-dictionary-by-key-or-value","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-sort-a-dictionary-by-key-or-value\/","title":{"rendered":"Python : How to Sort a Dictionary by Key or Value ?"},"content":{"rendered":"
Dictionaries are the implementation by Python of a data structure associative array. A dictionary is a collection of pairs of key values. A key pair and its associated value represent each key pair.<\/p>\n
The list of key value pairs in curly braces that is separated by comma defines a dictionary. Column ‘:’ separates the value of each key.<\/p>\n
A dictionary cannot be sorted only to get a representation of the sorted dictionary. Inherently, dictionaries are orderless, but not other types, including lists and tuples. Therefore, you need an ordered data type, which is a list\u2014probably a list of tuples.<\/p>\n
In this post, we will look at how to sort a dictionary by key or value<\/p>\n
Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to sort a dictionary by key or value some of them are:<\/p>\n A view object is returned by the items() method. The object view contains the dictionary’s key-value pairs as tuples in a list.<\/p>\n The object displays any modifications made in the dictionary.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Both of the previous solutions sorted the dictionary ascending order\u00a0by key. What if we were to sort the contents by the keys in decreasing order? This can be accomplished by simply passing an attribute to the sorted() function, such as reverse=True.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We’ll use the same sorted() function and transfer a key function that returns the first index element of the tuple, i.e. the value field from the key\/value pair, to sort dictionary elements by value.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n This can be accomplished by simply passing an attribute to the sorted() function, such as reverse=True.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Dictionaries are the implementation by Python of a data structure associative array. A dictionary is a collection of pairs of key values. A key pair and its associated value represent each key pair. The list of key value pairs in curly braces that is separated by comma defines a dictionary. Column ‘:’ separates the value …<\/p>\nBy keys<\/h3>\n
dictionary = {'this': 200, 'is':100, 'BTechGeeks':300}<\/pre>\n
BTechGeeks : 300\r\nis : 100\r\nthis :200<\/pre>\n
dictionary = {'this': 100, 'is':200, 'BTechGeeks':300}<\/pre>\n
is : 100\r\nthis : 200\r\nBTechGeeks : 300<\/pre>\n
Sort a Dictionary by Key\/Value<\/h2>\n
\n
Sort Dictionary by keys:<\/h3>\n
Method #1: Using items() function<\/h3>\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# convert the dictionary to list using items()\r\ndictitems = dictionary.items()\r\n# Using sorted() traverse this list\r\nfor i in sorted(dictitems):\r\n # print key and value\r\n print(i[0], \":\", i[1])\r\n<\/pre>\n
BTechGeeks : 300\r\nis : 100\r\nthis :200<\/pre>\n
Method #2:Using dict.keys() method<\/h3>\n
\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# convert the dictionary to list using dict.keys\r\ndictlist = list(dictionary.keys())\r\n# sort the list\r\ndictlist.sort()\r\n# Print the corresponding key and value by traversing this list\r\nfor key in dictlist:\r\n # print key and value\r\n print(key, \":\", dictionary[key])\r\n<\/pre>\n
BTechGeeks : 300\r\nis : 100\r\nthis :200<\/pre>\n
Method #3: Sorting the dictionary keys in reverse order<\/h3>\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# convert the dictionary to list using items()\r\ndictitems = dictionary.items()\r\n# Using sorted() traverse this list in descending order\r\nfor i in sorted(dictitems, reverse=True):\r\n # print key and value\r\n print(i[0], \":\", i[1])\r\n<\/pre>\n
this : 200\r\nis : 100\r\nBTechGeeks : 300<\/pre>\n
Sort Dictionary by Values:<\/h3>\n
Using sorted() and items() functions to sort dictionary by value<\/h3>\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# convert the dictionary to list using items()\r\ndictitems = dictionary.items()\r\n# Using sorted() traverse this list in\r\n# using key to sort by values\r\nfor i in sorted(dictitems, key=lambda x: x[1]):\r\n # print key and value\r\n print(i[0], \":\", i[1])\r\n<\/pre>\n
is : 100\r\nthis : 200\r\nBTechGeeks : 300<\/pre>\n
Using sorted() + items() +reverse() functions to sort dictionary by value in reverse order<\/h3>\n
# Given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# convert the dictionary to list using items()\r\ndictitems = dictionary.items()\r\n# Using sorted() traverse this list in\r\n# using key to sort by values\r\nfor i in sorted(dictitems, key=lambda x: x[1], reverse=True):\r\n # print key and value\r\n print(i[0], \":\", i[1])\r\n<\/pre>\n
BTechGeeks : 300\r\nthis : 200\r\nis : 100<\/pre>\n
\n