I need to print a python dictionary that is sorted by values. To sort the dictionary I used this code:
from operator import itemgetter
print(sorted(d.items(), key=itemgetter(1)))
The dictionary is well sorted, but the output is something like this:
[('Apple', '1'), ('Pears', '2'), ('Bananas', '3')]
I'd prefer:
Apple : 1
Pears : 2
Bananas : 3
I tried this wrong code:
dSorted = sorted(d.items(), key=itemgetter(1))
for i in dSorted:
print(i + " : "+ dSorted[i])
How can I get this result? And, to understand better, when I use (sorted(d.items(), key=itemgetter(1)))
the result is no longer a dictionary, but a different data structure, right?