Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort dictionary by key in numerical order Python

Tags:

Here is the dictionary looks like:

{'57481': 50, '57480': 89, '57483': 110, '57482': 18, '57485': 82, '57484': 40}   

I would like to sort the dictionary in numerical order, the result should be:

{'57480': 89, '57481': 50, '57482': 18, '57483': 110, '57484': 40, '57485': 82}  

I tried sorted(self.docs_info.items) but it doesn't work.

like image 918
Justin Avatar asked Mar 08 '14 04:03

Justin


People also ask

How do you sort a dictionary key numerically in Python?

To sort a dictionary by value in Python you can use the sorted() function. Python's sorted() function can be used to sort dictionaries by key, which allows for a custom sorting method. sorted() takes three arguments: object, key, and reverse. Dictionaries are unordered data structures.

Can we sort a dictionary with keys in Python?

Python offers the built-in keys functions keys() and values() functions to sort the dictionary. It takes any iterable as an argument and returns the sorted list of keys. We can use the keys to sort the dictionary in the ascending order.

How do you sort a dictionary in a list in Python by value?

To sort a list of dictionaries according to the value of the specific key, specify the key parameter of the sort() method or the sorted() function. By specifying a function to be applied to each element of the list, it is sorted according to the result of that function.


2 Answers

If you only need to sort by key, you're 95% there already. Assuming your dictionary seems to be called docs_info:

for key, value in sorted(docs_info.items()): # Note the () after items!     print(key, value) 

Since dictionary keys are always unique, calling sorted on docs_info.items() (which is a sequence of tuples) is equivalent to sorting only by the keys.

Do bear in mind that strings containing numbers sort unintuitively! e.g. "11" is "smaller" than "2". If you need them sorted numerically, I recommend making the keys int instead of str; e.g.

int_docs_info = {int(k) : v for k, v in docss_info.items()} 

This of course just changes the order in which you access the dictionary elements, which is usually sufficient (since if you're not accessing it, what does it matter if it's sorted?). If for some reason you need the dict itself to be "sorted", then you'll have to use collections.OrderedDict, which remembers the order in which items were inserted into it. So you could first sort your dictionary (as above) and then create an OrderedDict from the sorted (key, value) pairs:

sorted_docs_info = collections.OrderedDict(sorted(docs_info.items())) 
like image 130
Henry Keiter Avatar answered Feb 08 '23 22:02

Henry Keiter


Standard Python dicts are "unordered". You can use an OrderedDict, take a look at the docs:

from collections import OrderedDict  d = {'57481': 50, '57480': 89, '57483': 110, '57482': 18, '57485': 82, '57484': 40} OrderedDict(sorted(d.items(), key=lambda t: t[0])) # OrderedDict([('57480', 89), ('57481', 50), ('57482', 18), ('57483', 110), ('57484', 40), ('57485', 82)]) 
like image 41
elyase Avatar answered Feb 08 '23 23:02

elyase