Shorya Sharma
Shorya Sharma

Reputation: 457

Incorrect output for min and max in a list of dictionaries

I have a list of dictionaries like

t = [{'Sr. No.': '983', 'Hotel Code': 'TN05553556', 'State': 'Tamilnadu', 'Cost': '1110', 'Ratings': '5.3'}, {'Sr. No.': '984', 'Hotel Code': 'KA86403535', 'State': 'Karnataka', 'Cost': '1010', 'Ratings': '6.5'}, {'Sr. No.': '985', 'Hotel Code': 'TN23687028', 'State': 'Tamilnadu', 'Cost': '1790', 'Ratings': '5.5'}, {'Sr. No.': '986', 'Hotel Code': 'KA36970858', 'State': 'Karnataka', 'Cost': '1300', 'Ratings': '8.6'}, {'Sr. No.': '987', 'Hotel Code': 'MH38461439', 'State': 'Maharashtra', 'Cost': '2200', 'Ratings': '3'}, {'Sr. No.': '988', 'Hotel Code': 'KA20076818', 'State': 'Karnataka', 'Cost': '1340', 'Ratings': '7.1'}, {'Sr. No.': '989', 'Hotel Code': 'TN13118419', 'State': 'Tamilnadu', 'Cost': '910', 'Ratings': '6.2'}, {'Sr. No.': '990', 'Hotel Code': 'KA14225819', 'State': 'Karnataka', 'Cost': '470', 'Ratings': '2.6'}, {'Sr. No.': '991', 'Hotel Code': 'MH29527861', 'State': 'Maharashtra', 'Cost': '910', 'Ratings': '2.2'}, {'Sr. No.': '992', 'Hotel Code': 'MH29382347', 'State': 'Maharashtra', 'Cost': '2160', 'Ratings': '8.2'}, {'Sr. No.': '993', 'Hotel Code': 'MH11989011', 'State': 'Maharashtra', 'Cost': '2110', 'Ratings': '2'}, {'Sr. No.': '994', 'Hotel Code': 'KA25450392', 'State': 'Karnataka', 'Cost': '1450', 'Ratings': '9'}, {'Sr. No.': '995', 'Hotel Code': 'KA66177616', 'State': 'Karnataka', 'Cost': '2030', 'Ratings': '2.3'}, {'Sr. No.': '996', 'Hotel Code': 'MH45682094', 'State': 'Maharashtra', 'Cost': '1340', 'Ratings': '2.4'}, {'Sr. No.': '997', 'Hotel Code': 'TN79155458', 'State': 'Tamilnadu', 'Cost': '1620', 'Ratings': '4.6'}, {'Sr. No.': '998', 'Hotel Code': 'KA26726731', 'State': 'Karnataka', 'Cost': '850', 'Ratings': '8'}, {'Sr. No.': '999', 'Hotel Code': 'TN09700536', 'State': 'Tamilnadu', 'Cost': '1550', 'Ratings': '5.3'}, {'Sr. No.': '1000', 'Hotel Code': 'MH38559747', 'State': 'Maharashtra', 'Cost': '600', 'Ratings': '4'}]

and to calculate the maximum cost I have used the following

maxPricedItem = max(t, key=lambda x:x["Cost"])

and it should ideally give the correct max but the Output is

maxPricedItem['Cost'] = 910

which is clearly wrong . I am unable to wrap my head around why this is happening. It works when the length of the list of dictionaries is comparitively small. A little guidance please.

Upvotes: 1

Views: 27

Answers (2)

DirtyBit
DirtyBit

Reputation: 16782

And if the intention is to only get the max value, using list-comprehension:

print(max([int(x['Cost']) for x in t]))  # 2200

Upvotes: 0

Andrej Kesely
Andrej Kesely

Reputation: 195543

You are comparing strings, so convert the Cost to integer in your key function:

t = [{'Sr. No.': '983', 'Hotel Code': 'TN05553556', 'State': 'Tamilnadu', 'Cost': '1110', 'Ratings': '5.3'}, {'Sr. No.': '984', 'Hotel Code': 'KA86403535', 'State': 'Karnataka', 'Cost': '1010', 'Ratings': '6.5'}, {'Sr. No.': '985', 'Hotel Code': 'TN23687028', 'State': 'Tamilnadu', 'Cost': '1790', 'Ratings': '5.5'}, {'Sr. No.': '986', 'Hotel Code': 'KA36970858', 'State': 'Karnataka', 'Cost': '1300', 'Ratings': '8.6'}, {'Sr. No.': '987', 'Hotel Code': 'MH38461439', 'State': 'Maharashtra', 'Cost': '2200', 'Ratings': '3'}, {'Sr. No.': '988', 'Hotel Code': 'KA20076818', 'State': 'Karnataka', 'Cost': '1340', 'Ratings': '7.1'}, {'Sr. No.': '989', 'Hotel Code': 'TN13118419', 'State': 'Tamilnadu', 'Cost': '910', 'Ratings': '6.2'}, {'Sr. No.': '990', 'Hotel Code': 'KA14225819', 'State': 'Karnataka', 'Cost': '470', 'Ratings': '2.6'}, {'Sr. No.': '991', 'Hotel Code': 'MH29527861', 'State': 'Maharashtra', 'Cost': '910', 'Ratings': '2.2'}, {'Sr. No.': '992', 'Hotel Code': 'MH29382347', 'State': 'Maharashtra', 'Cost': '2160', 'Ratings': '8.2'}, {'Sr. No.': '993', 'Hotel Code': 'MH11989011', 'State': 'Maharashtra', 'Cost': '2110', 'Ratings': '2'}, {'Sr. No.': '994', 'Hotel Code': 'KA25450392', 'State': 'Karnataka', 'Cost': '1450', 'Ratings': '9'}, {'Sr. No.': '995', 'Hotel Code': 'KA66177616', 'State': 'Karnataka', 'Cost': '2030', 'Ratings': '2.3'}, {'Sr. No.': '996', 'Hotel Code': 'MH45682094', 'State': 'Maharashtra', 'Cost': '1340', 'Ratings': '2.4'}, {'Sr. No.': '997', 'Hotel Code': 'TN79155458', 'State': 'Tamilnadu', 'Cost': '1620', 'Ratings': '4.6'}, {'Sr. No.': '998', 'Hotel Code': 'KA26726731', 'State': 'Karnataka', 'Cost': '850', 'Ratings': '8'}, {'Sr. No.': '999', 'Hotel Code': 'TN09700536', 'State': 'Tamilnadu', 'Cost': '1550', 'Ratings': '5.3'}, {'Sr. No.': '1000', 'Hotel Code': 'MH38559747', 'State': 'Maharashtra', 'Cost': '600', 'Ratings': '4'}]

maxPricedItem = max(t, key=lambda x:int(x["Cost"]))

print(maxPricedItem)

Prints:

{'Sr. No.': '987', 'Hotel Code': 'MH38461439', 'State': 'Maharashtra', 'Cost': '2200', 'Ratings': '3'}

Upvotes: 2

Related Questions