return max value from a list of dictionary in python

Question:

Update: I have already accepted the answer. However, if anyone could explain about why I got error message "keyerror:1 " in my method 2 in the question, I will be very happy with that. Thank you!

—————-original question —————-

I ‘m completing one exercise, but whatever I have tried, nothing was correct. I went through all those available answers, still I could not figure it out. The expected answer is described hereinbelow:

The exercise template contains a class named Car which represents the features of a car through two attributes: make (str) and top_speed (int).

Please write a function named fastest_car(cars: list) which takes a list of Car objects as its argument.

The function should return the make of the fastest car. You may assume there will always be a single car with the highest top speed. Do not change the list given as an argument, or make any changes to the Car class definition.

You may use the following code to test your function:

if __name__ == "__main__":
    car1 = Car("Saab", 195)
    car2 = Car("Lada", 110)
    car3 = Car("Ferrari", 280)
    car4 = Car("Trabant", 85)

    cars = [car1, car2, car3, car4]
    print(fastest_car(cars))

# Ferrari

I have tried like this:



class Car:
  def __init__(self,make:str,top_speed:int):
    self.make=make
    self.top_speed=top_speed


  def fastest_car(self,list):
    list.sort(key = lambda x : x.top_speed)
    print("Sort by speed")
    print(list)
  

if __name__ == "__main__":
    car1 = Car("Saab", 195)
    car2 = Car("Lada", 110)
    car3 = Car("Ferrari", 280)
    car4 = Car("Trabant", 85)

    cars = [car1, car2, car3, car4]
   
    Car.fastest_car(cars[-1])


#error message: TypeError: fastest_car() missing 1 required positional argument: 'list'

I also tried normal method:

#method 2

cars=[{'Saab':195},{'Lada':110},{'Ferrari':280},{'Trabant':85}]

a=sorted(cars,key = lambda x : x[1])
print(a)#just print the sorted list

error message:


----> 3 a=sorted(cars,key = lambda x : x[1])
      4 print(a)

KeyError: 1

If you could kindly pointed out what I did wrong, I will highly appreciated. Thank You!

point out mistakes and gain the knowledge

Asked By: stel

||

Answers:

So thanks to @12944qwerty and @Barmer’s help, this problem is solved like this:

class Car:
  def __init__(self,make:str,top_speed:int):
    self.make=make
    self.top_speed=top_speed

  @staticmethod
  def fastest_car(list):
    list.sort(key = lambda x : x.top_speed)
    print("Sort by speed")
    return list[-1]
    
  def __repr__(self):
    return self.make


if __name__ == "__main__":
    car1 = Car("Saab", 195)
    car2 = Car("Lada", 110)
    car3 = Car("Ferrari", 280)
    car4 = Car("Trabant", 85)

    cars = [car1, car2, car3, car4]
  
    print(Car.fastest_car(cars))

#result 

Sort by speed
Ferrari

I tried also to take that @staticmethod away and I still get the same result. I hope that somebody could explain a little bit why and if possible, someone could tell me what the error message means in that normal method I tried, KeyError: 1
Thanks!

Answered By: stel

just use max() function:

fastest_car = max(cars, key=lambda x: x.top_speed)
print(fastest_car.make)
print(fastest_car.top_speed)

>>> out
'''
Ferrari
280

UPD

for list of dictionaries you can try this:

cars=[{'Saab':195},{'Lada':110},{'Ferrari':280},{'Trabant':85}]

d = {}
for i in cars:
    d.update(i)

max(d,key=d.get)  # 'Ferrari'
Answered By: SergFSM
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.