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

0 votes

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

Solution output
file

Expected output
file: 50

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

Solution output
mobile4

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
final_list=[]
#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 )
#input_dict.get('Label','NA') 
# list out keys and values separately 
#key_list = list(my_dict.keys()) 
#val_list = list(my_dict.values()) 
#print(key_list[val_list.index(100)]) 
#print(key_list[val_list.index(112)]) 
#print(list(input_dict.keys())[list(input_dict.values()).index(9000)]) 

Any one correct my code. Thanks in advance

Jun 20, 2020 in Python by rocking
• 160 points
3,665 views

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
items=ast.literal_eval(input())

#start writing your code here

#we assign cheapest_item as first key of the items
cheapest_item=list(items.keys())[0]

#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
1,190 views
0 votes
2 answers

how to print the current time using python?

print(datetime.datetime.today()) READ MORE

answered Feb 14, 2019 in Python by Shashank
• 1,370 points
1,130 views
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
• 3,050 points
2,393 views
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
• 33,030 points
1,053 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP