To get just the maximum value and not the entire object you can use a generator expression:

print(max(node.y for node in path.nodes))
Answer from Mark Byers on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-get-the-object-with-the-max-attribute-value-in-a-list-of-objects
Python - Get the object with the max attribute value in a list of objects - GeeksforGeeks
July 23, 2025 - This task can be achieved by using the max() method and attrgetter operator. It returns a callable object that fetches attr from its operand. If more than one attribute is requested, returns a tuple of attributes.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-get-the-object-with-the-max-attribute-value-in-a-list-of-objects
Python - Get the object with the max attribute value in a list of objects
July 18, 2023 - In Python, we can get the object with a max attribute value in a list of objects using the max() function with a lambda function, using the operator module, using a custom comparison function, etc. In this article, we will explore different methods t
Discussions

python - Get the object with the max attribute value in a list of objects - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... Closed 11 years ago. nodes_ancestors is a list of objects with a level attribute. I would like to get the object from the list with the highest level value, but I just get the highest value with: ancestor = max... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Get the object with the max attribute's value in a list of objects - Stack Overflow
This returns the matching object for which that attribute is the maximum. If you wanted to store just that maximum value itself, you have two options: ... If you find that you need to order the One classes in various directions by the same attribute again and again (to find the minimum, maximum, sort them, etc.) you may want to make your class orderable as well. To do that, you'll need to implement some of the basic customization hooks Python ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to find maximum values from a list which contains objects in Python? - Stack Overflow
I have a class: class Point(object): def __init__(self, x, y): self.x = x self.y = y I create a list of objects of the above class Point which has field x and y. I want to find More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 2, 2017
python - Get a list of objects with the maximum attribute value in a list of objects - Stack Overflow
Slightly different from previous questions. I have found here: front_Ar is a list of objects with a score attribute. I am trying to get a list of all objects with the highest score. I have tried: More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 1
46

max() takes a key parameter, a function that when passed one of the objects returns the value by which to compare them.

Use operator.attrgetter() to get that value:

from operator import attrgetter

max(self.allPartners, key=attrgetter('attrOne'))

This returns the matching object for which that attribute is the maximum. If you wanted to store just that maximum value itself, you have two options:

  • Take the attribute from the returned object:

    max(self.allPartners, key=attrgetter('attrOne')).attrOne
    
  • Pass just the attributes instead to max() with a generator expression:

    max(p.attrOne for p in self.allPartners)
    

If you find that you need to order the One classes in various directions by the same attribute again and again (to find the minimum, maximum, sort them, etc.) you may want to make your class orderable as well.

To do that, you'll need to implement some of the basic customization hooks Python will look for. With some extra trickery, you can get away with just the lower-than and equals operations, and by using the funtools.total_ordering class decorator:

from functools import total_ordering

@total_ordering
class One:
    # ...
    def __lt__(self, other):
        if not isinstance(other, type(self)): return NotImplemented
        return self.attrOne < other.attrOne

    def __eq__(self, other):
        if not isinstance(other, type(self)): return NotImplemented
        return self.attrOne == other.attrOne

Now your One class is orderable, entirely on the basis of attrOne; for the max() function, that means you can drop the key parameter altogether.

๐ŸŒ
Tutorial Reference
tutorialreference.com โ€บ python โ€บ examples โ€บ faq โ€บ python-how-to-get-object-with-max-attribute-value-in-list
How to Get the Object with the Max Attribute Value in a List of Objects in Python | Tutorial Reference
When working with lists of objects ... with the highest salary, the product with the best rating, or the student with the top score. Python's built-in max() function makes this straightforward when combined with a key function....
๐ŸŒ
Pythonista Planet
pythonistaplanet.com โ€บ max-and-min-of-an-attribute-in-a-list-of-objects
Finding the Max/Min of an Attribute in a List of Objects in Python โ€“ Pythonista Planet
June 23, 2022 - You can do this in two ways. Either you can manually write the code to compare the price values of all the objects and find out the maximum value, or you can use the max() method in Python.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-search-the-max-value-of-an-attribute-in-an-array-object
How to search the max value of an attribute in an array object ?
In this method, we iterate through the list of objects using a loop and compare the attribute value of each object with the current maximum value. for obj in objects: if obj.attribute > max_value: max_value = obj.attribute ยท Here, inside the ...
Find elsewhere
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ get-the-object-with-the-max-attribute39s-value-in-a-list-of-objects-in-python.html
Get the object with the max attribute's value in a list of objects in python
We use the max() function with a lambda function as the key argument to specify that we want to find the object with the maximum value attribute. The key function extracts the value attribute for each object in the list. Finally, we access the value attribute of the object with the maximum ...
๐ŸŒ
GeeksforGeeks
geofranz.com โ€บ www.geeksforgeeks.org โ€บ python-get-the-object-with-the-max-attribute-value-in-a-list-of-objects
Python - Get the object with the max attribute value in a list of objects - GeeksforGeeks
January 4, 2022 - This task can be achieved by using the max() method and attrgetter operator. It returns a callable object that fetches attr from its operand. If more than one attribute is requested, returns a tuple of attributes. The attribute names can also contain dots. ... To get the job done, define a ...
๐ŸŒ
Iditect
iditect.com โ€บ programming โ€บ python-example โ€บ python-get-the-object-with-the-max-attribute-value-in-a-list-of-objects.html
Python - Get the object with the max attribute value in a list of objects
Use the max() function with a lambda to specify the attribute for comparison. ... class Person: def __init__(self, name, age): self.name = name self.age = age def __repr__(self): return f"Person(name='{self.name}', age={self.age})" # Create a list of Person objects people = [ Person("Alice", ...
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ get-the-object-with-the-max-attribute-value-in-a-list-of-objects
How to Get the Object with the Maximum Attribute Value from a Python List of Objects (Instead of Just the Max Value) โ€” pythontutorials.net
Goal: Find the Product object with the highest price (in this case, the "Laptop" with price=999.99). The simplest way to retrieve the object with the maximum attribute value is to use Pythonโ€™s built-in max() function with a key parameter.
๐ŸŒ
DNMTechs
dnmtechs.com โ€บ finding-maximum-attribute-values-in-a-list-of-objects-in-python-3
Finding Maximum Attribute Values in a List of Objects in Python 3 โ€“ DNMTechs โ€“ Sharing and Storing Technology Knowledge
In this example, we import the operator module and use the attrgetter() function to specify the attribute we want to compare. The max() function then returns the object with the maximum attribute value, similar to the previous approach. In this article, we explored different approaches to finding ...
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ getting-the-max-value-of-attributes-from-a-list-of-objects-in-python.html
Getting the max value of attributes from a list of objects in python
# Code Implementation: max_value = max(objects, key=lambda x: getattr(x, 'value')).value # Usage Example: class MyClass: def __init__(self, value): self.value = value objects = [MyClass(10), MyClass(20), MyClass(5)] print("Maximum value:", max_value) # Output: Maximum value: 20 ยท "Python find highest attribute value in a list of objects efficiently" Description: This query suggests the user is concerned about efficiency and seeks an optimized approach to finding the highest attribute value within a list of objects.
๐ŸŒ
w3reference
w3reference.com โ€บ blog โ€บ pythonic-way-to-get-the-largest-item-in-a-list
How to Get the Largest Item in a Python List by Attribute: A Readable and Efficient Pythonic Approach โ€” w3reference.com
We need to write code that returns the Product object with the largest price (in this case, the "Gaming PC"). A naive approach is to iterate through the list with a for loop, tracking the item with the maximum attribute value.