Darien
Darien

Reputation: 113

Sorting dictionary descending in Python

I am new to Python and I was wondering if there is a more common way of sorting a dictionary in descending order than the way I'm doing it:

sorted_dictionary = sorted(dict.items(), key=lambda x: -x[1])

Upvotes: 11

Views: 32604

Answers (6)

thereal90
thereal90

Reputation: 105

This should work

{k: v for k, v in sorted(dict.items(), key=lambda item: item[1], reverse = True)}

Upvotes: 2

Michael Fitzgerald
Michael Fitzgerald

Reputation: 11

Here is code that get all the prime factors with exponents for all numbers in a range

    from sympy import primefactors

for num in range (1,10001): k = num prime_factors = {}

for i in primefactors(num):
    count = 0
    while num % i == 0:
        num = num/i
        count += 1
    prime_factors[i] = count  

result = sorted(prime_factors.items() , key=lambda t : t[0] , reverse=True)

count = 1
num = k

print '\n%d can be broken down into the following prime factors:' % num,
print '\n%d = ' % num,
for k,v in result:
    if count < len(result):
        print '(%d ** %d) *' % (k,v),
        count += 1
    else:
        print '(%d ** %d)' % (k,v)

Upvotes: 0

Fuji Komalan
Fuji Komalan

Reputation: 2047

Sorting Dictionary based on values in ascending order

d = {'a':5 , 'b':3 , 'd':1 , 'e':2 , 'f':4}
result = sorted(d.items() , key=lambda t : t[1])

for k,v in result:
  print(k,v)

RESULT

d 1
e 2
b 3
f 4
a 5


# d.items=[('a', 5), ('b', 3), ('d', 1), ('e', 2), ('f', 4)]
# lambda t = ('a',5)
# lambda t : t[1] = 5
# result = [('d', 1), ('e', 2), ('b', 3), ('f', 4), ('a', 5)]
# k ,v = ('d',1)
# k = 'd'  
# v = 1

Sorting Dictionary based on values in descending order

d = {'a':5 , 'b':3 , 'd':1 , 'e':2 , 'f':4}
result = sorted(d.items() , key=lambda t : t[1] , reverse=True)

for k,v in result:
  print(k,v)

RESULT

 a 5
 f 4
 b 3
 e 2
 d 1


# d.items=[('a', 5), ('b', 3), ('d', 1), ('e', 2), ('f', 4)]
# lambda t = ('a',5)
# lambda t : t[1] = 5
# result = [('a', 5), ('f', 4), ('b', 3), ('e', 2), ('d', 1)]
# k ,v = ('a',5)
# k = 'a'  
# v = 5

Upvotes: 0

Garima Chhikara
Garima Chhikara

Reputation: 29

Sort in decreasing order of values in python list:

items = {'a':6 , 'b':3 , 'c':1 , 'd':9}
sortedItems = sorted(items , key=items.get , reverse = True)
for element in sortedItems :
       print (element, items.get(element))

Output is:

d 9
a 6
b 3
c 1

Upvotes: -1

Francesco Nazzaro
Francesco Nazzaro

Reputation: 2916

Python dictionary aren't sortable. Your sorted_dictionary output is not a dictionary but a list. You have to use OrderedDict

from collections import OrderedDict

sorted_dictionary = OrderedDict(sorted(dict.items(), key=lambda v: v, reverse=True))

Upvotes: 8

Martijn Pieters
Martijn Pieters

Reputation: 1121486

There is a reverse option to sorted() you could use instead:

sorted(dict.items(), key=lambda kv: kv[1], reverse=True)

This produces the exact same output, and even works if the values are not numeric.

Upvotes: 22

Related Questions