I want to reverse a dictionary and display it in a specific format. Here is the sample input:
{'john':34.480, 'eva':88.5, 'alex':90.55, 'tim': 65.900}
Output should be:
This is where I am with the code, but the problem is that it returns a list and not a dictionary.
CODE:
def formatted_print(my_dict):
d = my_dict
c = sorted(d.items(), cmp=lambda a,b: cmp(a[1], b[1]), reverse=True)
return (c)
Simple code without importing any module or library :
def formatted_print(D):
list_tuples=sorted(D.items(), key=lambda x: (-x[1], x[0]))
for items in list_tuples:
x="{0:10s}{1:6.2f}".format(items[0],items[1])
print(x)
It prints:
alex 90.55
eva 88.50
tim 65.90
john 34.48
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments