1
list1 = ['AMZN', 'CACC', 'EQIX', 'GOOG', 'ORLY', 'ULTA']
list2 = [[12.81, 11.09, 12.11, 10.93, 9.83, 8.14], [10.34, 10.56, 10.14, 12.17, 13.10,11.22], [12.81, 11.09, 12.11, 10.93, 9.83, 8.14]]
m = [sum(i) for i in zip(*list2)]

culo = []
for ss in m:
    culo.append(ss/3)

zip_iterator = zip(list1, culo)
a_dictionary = dict(zip_iterator)
print(a_dictionary)

Hello everyone, do you have any clue how to order the dictionary "a_dictionary" in biggest number to the smallest from the value?

  • Check out this question: https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value – DSteman May 27 '22 at 16:02

2 Answers2

1

You can use a dictionary comprehension using sorted(), passing in a key parameter to sort by value, and a reverse parameter to sort in descending order:

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

This outputs:

{
 'AMZN': 11.986666666666666,
 'EQIX': 11.453333333333333,
 'GOOG': 11.343333333333334,
 'ORLY': 10.92,
 'CACC': 10.913333333333332,
 'ULTA': 9.166666666666666
}
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
1

I will go a simpler looking solution:

m = ....
culo = [ (ss/3, l1) for ss, l1 in zip(m, list1)]
print(sorted(culo, reverse=True))

returns:

[(9.166666666666666, 'ULTA'),
 (10.913333333333332, 'CACC'),
 (10.92, 'ORLY'),
 (11.343333333333334, 'GOOG'),
 (11.453333333333333, 'EQIX'),
 (11.986666666666666, 'AMZN')]
Leonardo
  • 94
  • 1
  • 7