Answer to Question #298545 in Python for Michael

Question #298545

On Sunday, Sunil went to the D-Mart with his family. All the family members of Sunil buy many items as per their need. At the payment counter Sunil paid 5000 Rs. After payment Sunil came home and now, he wants to analyse the top three item he bought from the D-Mart. You need to write a python function Top_Price(A)which takes a dictionary containing all items with their prices and return a string containing top-3 item names and their prices as shown in the example

 

Example-1

Example-2

Input:

{'item1': 45.50, 'item2':35, 'item3': 41.30, 'item4':55, 'item5': 24}

 

Output:

item4-55

item1-45.5

item3-41.3

Input:

{'item1': 105.50, 'item2':325, 'item3': 411.30, 'item4':5.4, 'item5': 24.7}

 

Output:

Item3-411.30

Item2-325

Item1-105.50



1
Expert's answer
2022-02-16T10:21:10-0500
x = {'item1': 105.50, 'item2': 325, 'item3': 411.30, 'item4': 5.4, 'item5': 24.7}
x = {k: v for k, v in sorted(x.items(), key=lambda item: item[1], reverse=True)}
for i in range(0, 3):
    print(f'{list(x.keys())[i]}-{list(x.values())[i]}')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS