Get a product with the highest price from a dictionary.

In this python dictionary program, we will get a product with the highest price from a dictionary.

Steps to solve the program
  1. Take a dictionary of products as input.
  2. Create two variables to store the product name and the product price.
  3. Assign their values equal to 0.
  4. Use for loop to iterate over keys and values of the input dictionary using items().
  5. If the value of the product is greater than the variable that we have created to store the product price and name, then assign the value of the variable to the corresponding product name and price.
  6. Repeat this process for all the products
  7. Print the output.
				
					dict1 = {'price1':450,'price2':600,'price3':255,'price4':400}
p_name = 0
p_price = 0

for key,val in dict1.items():
    if val > p_price:
        p_price = val
        p_name = key

print("Product name: ",p_name)
print("Product price: ",p_price)
				
			

Output :

				
					Product name:  price2
Product price:  600
				
			

sort a list of values in a dictionary.

print a dictionary line by line.

Leave a Comment