1

I have a dictionary that the keys representing the item and the value represent count of that. for example in below dictionary:

dict= {'11': 4, '0': 2, '65': 1, '88': 1, '12': 1, '13': 1}

'11' occurred 4 times
'0' occurred 2 times
'65' occurred 1 time

How to order the dictionary that dict.keys() are descending or ascending?

The Ideal out put will be either

dict={'0':2,'11':4,'12':1,'13':1,'65':1,'88':1} 

or

dict={'88':1,'65':1,'13':1,'12':1,'11':4,'0':2}

Any help would be appreciated

Nataliya
  • 79
  • 7
  • Did you remember to [search](https://stackoverflow.com/search) before posting? https://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key – costaparas Feb 21 '21 at 05:03
  • 2
    Also, please don't use `dict` as a variable name, its a builtin already. – costaparas Feb 21 '21 at 05:03
  • There is a special case version of dict called [collections.Counter](https://docs.python.org/3/library/collections.html#collections.Counter). Might that be useful here? Also do you want the dict to be actually ordered or are you only looking to iterate it in ascending or descending order? – Paul Rooney Feb 21 '21 at 05:14

5 Answers5

2

score = {'eng': 33, 'sci': 85, 'math': 60}

You can do it like this...

score_sorted = sorted(score.items(), key=lambda x:x[0])

If you wanna sort it by val, then score_sorted = sorted(score.items(), key=lambda x:x[1]). You can add reverse=True to change order as well.

Pytan
  • 1,138
  • 11
  • 28
  • We'll want to call dict(score_sorted) so transform the tuples back into a dictionary. Also, @Pytan, I don't thing we need to pass a key if we're just sorting by the key, as I believe that is the default. – dtluther Feb 21 '21 at 05:39
1

Note: Don't use dict as a variable name as it is already a built-in function.

your_dict = {'11': 4, '0': 2, '65': 1, '88': 1, '12': 1, '13': 1} is better.

You can use sample_list = list(your_dict.items()) which convets the given dict into a list.

In the Python Dictionary, items() method is used to return the list with all dictionary keys with values.

Use sample_list.sort() to sort the list.

To reverse the list, use reverse = True

sample_list = list(your_dict.items())
sample_list.sort(reverse = True)

Then use dict = dict(sample_list) to convert it into a dictionary and print it out.

1
myDict= {'11': 4, '0': 2, '65': 1, '88': 1, '12': 1, '13': 1}
sortDict = {}

for i in sorted(myDict.keys()) :  
     sortDict[i] = myDict[i]

print(sortDict) 
Wrench
  • 490
  • 4
  • 13
1

Contrary to older posts dictionaries are no longer unordered and can be sorted since CPython 3.6 (unofficially, as a C implementation detail) and Python 3.7 (officially).

To sort by key use a dictionary comprehension to build a new dictionary in the order desired. If you want to sort by string collation order, use the following, but note that '2' comes after '11' as a string:

>>> d = {'11': 4, '2': 2, '65': 1, '88': 1, '12': 1, '13': 1}
>>> {k:d[k] for k in sorted(d)}
{'11': 4, '12': 1, '13': 1, '2': 2, '65': 1, '88': 1}

To order by integer value, pass a key function that converts the string to an integer:

>>> {k:d[k] for k in sorted(d,key=lambda x: int(x))}
{'2': 2, '11': 4, '12': 1, '13': 1, '65': 1, '88': 1}

Or reversed you can use reverse=True or just negate the integer:

>>> {k:d[k] for k in sorted(d,key=lambda x: -int(x))}
{'88': 1, '65': 1, '13': 1, '12': 1, '11': 4, '2': 2}

With older Python versions convert the dictionary to a list with list(d.items()) and use similar sorting.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
1
dict= {'11': 4, '0': 2, '65': 1, '88': 1, '12': 1, '13': 1}

You can try dictionary comprehension like this

sorted_dict={k:dict[k] for k in sorted(dict)}
print(sorted_dict)
Pyd
  • 6,017
  • 18
  • 52
  • 109