You have to print the cheapest item using the dictionary key and values

0 votes

{'laptop': 30000, 'file': 50}

Solution output

Expected output
file: 50

{'mobile1':10000, 'mobile2':11000, 'mobile3':13000, 'mobile4':9000, 'mobile5':15000, 'mobile6':16000, 'mobile7':17000, 'mobile8':18000, 'mobile9':19000}

Solution output

Expected output
mobile4: 9000

My Wrong Code:

#take input here
#input has been taken for you
import ast
import operator
input_str = input()
#input dictionary has been received in input_dict
input_dict = ast.literal_eval(input_str)
#start writing your code here
#print(min(input_dict.items(), key=operator.itemgetter(1))[0])
#Cheapest = min(input_dict.items(), key=lambda i: i[1])
#Cheapest = min(input_dict, key=input_dict.get,values=input_dict.get )
Cheapest = min(input_dict, key=input_dict.get)
#print('Cheapest: {}'.format(Cheapest[input_dict]))
print(Cheapest )
# list out keys and values separately 
#key_list = list(my_dict.keys()) 
#val_list = list(my_dict.values()) 

Any one correct my code. Thanks in advance

Jun 19, 2020 in Python by rocking
• 160 points

Hi, @Rocking,

Are you facing any kind of error while executing this code? Could you please post the error logs if anything is coming to you?

1 answer to this question.

0 votes
take input here
import ast

#start writing your code here

#we assign cheapest_item as first key of the items

#we check every key and see if its cheaper than our cheapest_item
for key in items.keys():
    if items[key]<items[cheapest_item]:
        cheapest_item=key #it its cheaper then we update out cheapest_item

#print it
print('{0}: {1}'.format(cheapest_item, items[cheapest_item]))
answered Dec 19, 2020 by anonymous

Related Questions In Python

0 votes
1 answer
+1 vote
1 answer
+1 vote
0 answers

Sum the values of column matching and not matching from a .txt file and write output to a two different files using Python

Name                                                    value DR_CNDAOFSZAPZP_GPFS_VOL.0 139264 DR_CNDAOFSZAPZP_GPFS_VOL.1 15657 DR_CNDAOFSZAPZP_GPFS_VOL.0 139264 DR_CNDAOFSZAPZP_GPFS_VOL.1 156579 DR_CNDAOFSZAPZP_GPFS_VOL.2 156579 DR_CNDAOFSZAPZP_GPFS_VOL.3 ...READ MORE

Nov 20, 2019 in Python by Sagar
• 130 points
0 votes
2 answers

how to print the current time using python?

print( READ MORE

answered Feb 14, 2019 in Python by Shashank
• 1,370 points
0 votes
1 answer

How to access the elements of a dictionary using index ?

Suppose you have a dictionary num = ...READ MORE

answered Jul 4, 2019 in Python by Arvind
• 2,980 points
0 votes
1 answer

How to have values from a List1 into Columns Names and List 2 Values as Index for an Empty DataFrame

Try like this, it will give you ...READ MORE

answered Jul 10, 2019 in Python by Cherukuri
• 32,910 points