Get the object with Max attribute value in List of Objects in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Get the object with Max attribute value in List of Objects in Python #

Use the max() function to get the object with the max attribute value in a list of objects. The max() function can be passed a key argument that points to the attribute to be compared and will return the object with the max attribute value in the list.

main.py
from operator import attrgetter class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __repr__(self): return self.name alice = Employee('August', 100) bob = Employee('Bobbyhadz', 150) carl = Employee('Carl', 75) list_of_objects = [alice, bob, carl] # ✅ get the object with the max attribute value in a list of objects matching_obj = max(list_of_objects, key=attrgetter('salary')) print(matching_obj) # 👉️ Bobbyhadz print(matching_obj.name) # 👉️ Bobbyhadz print(matching_obj.salary) # 👉️ 150 # ✅ get the max attribute value in a list of objects max_salary = max(obj.salary for obj in list_of_objects) print(max_salary) # 👉️ 150

We used the max() function to get the object with the max attribute value in a list of objects.

The max function returns the largest item in an iterable or the largest of two or more arguments.

The max function also takes an optional key argument.

main.py
matching_obj = max(list_of_objects, key=attrgetter('salary')) print(matching_obj) # 👉️ Bobbyhadz print(matching_obj.name) # 👉️ Bobbyhadz print(matching_obj.salary) # 👉️ 150

The key argument specifies a one-argument ordering function.

We used the operator.attrgetter method.

The method returns a callable object that returns the specified attribute value.

You can imagine that:

  1. The attrgetter() method gets called for each object in the list.
  2. The method returns the value of the salary attribute of each object.
  3. The max() function returns the object with the max attribute value.

Alternatively, you can use a lambda function.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __repr__(self): return self.name alice = Employee('August', 100) bob = Employee('Bobbyhadz', 150) carl = Employee('Carl', 75) list_of_objects = [alice, bob, carl] matching_obj = max(list_of_objects, key=lambda obj: obj.salary) print(matching_obj) # 👉️ Bobbyhadz print(matching_obj.name) # 👉️ Bobbyhadz print(matching_obj.salary) # 👉️ 150

We set the key argument to a lambda function instead of using the attrgetter() method.

Which approach you pick is a matter of personal preference. The difference in performance is likely going to be negligible.

Get the max attribute value in a list of objects #

To get the max attribute value in a list of objects:

  1. Use a generator expression to iterate over the list.
  2. On each iteration, access the specific attribute and return the result.
  3. Use the max() function to get the max attribute value.
main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __repr__(self): return self.name alice = Employee('August', 100) bob = Employee('Bobbyhadz', 150) carl = Employee('Carl', 75) list_of_objects = [alice, bob, carl] max_salary = max(obj.salary for obj in list_of_objects) print(max_salary) # 👉️ 150

We used a generator expression to get a generator object that contains the values of the salary attributes of the objects in the list.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we access the salary attribute on the object and return the result.

The max() function returns the max of the values.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.