203k views
0 votes
: Write a Python program to get the top three items price in a shop. Sample data: {'Apple': 0.50, 'Banana': 0.20, 'Mango': 0.99, 'Coconut': 2.99, 'Pineapple': 3.99}

Expected Output:
Pineapple 3.99
Coconut 2.99
Mango 0.99

User Jolie
by
8.4k points

2 Answers

6 votes

Final answer:

To get the top three items' prices in a shop, use Python's built-in functions to sort the dictionary items by values and then select the top three entries.

Step-by-step explanation:

To get the top three items' prices in a shop, you can use Python's built-in functions to sort the dictionary items by values and then select the top three entries. Here's how you can do it:

prices = {'Apple': 0.50, 'Banana': 0.20, 'Mango': 0.99, 'Coconut': 2.99, 'Pineapple': 3.99}

# Sort the dictionary items by values in descending order
sorted_prices = sorted(prices.items(), key=lambda x: x[1], reverse=True)

# Select the top three entries
top_three = sorted_prices[:3]

# Print the top three items and their prices
for item, price in top_three:
print(item, price)

The expected output for the provided sample data would be:

Pineapple 3.99
Coconut 2.99
Mango 0.99

User Sabaz
by
8.9k points
2 votes

This is a Python program that retrieves the top three items and their prices from the given shop data:

How to write the Python code

shop_data = {'Apple': 0.50, 'Banana': 0.20, 'Mango': 0.99, 'Coconut': 2.99, 'Pineapple': 3.99}

# Sort the items by price in descending order

sorted_items = sorted(shop_data.items(), key=lambda x: x[1], reverse=True)

# Display the top three items and their prices

print("Expected Output:")

for item, price in sorted_items[:3]:

print(f"{item} {price}")

User Leo Loki
by
7.4k points