Python - Get the object with the max attribute value in a list of objects
Last Updated :
14 Feb, 2023
Given a list of objects, the task is to write a Python program to get the object with the max attribute value in a list of objects using Python.
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.
Syntax:
attrgetter(attr)
To get the job done, define a class and define a __init__() method. Then, initialize the instance variables for example salary and experience. Now, create multiple instances/ Objects for the geeks class and append all the objects into a list. Now pass the list of objects to the max() method with key=attribute, The attribute can be given using the attrgetter operator.
The max() method will return the object with max attribute value.
Example 1: Get the object with max attribute value in a list of objects
Python3
from operator import attrgetter
class geeks:
def __init__(self, experience, salary):
self.experience = experience
self.salary = salary
object1 = geeks(2, 50000)
object2 = geeks(3, 60000)
object3 = geeks(1, 20000)
object4 = geeks(4, 75000)
l = [object4, object3, object2, object1]
max_attr = max(l, key=attrgetter('salary'))
print(max_attr.salary)
Time Complexity: O(n), where n is the total number of objects in the list.
Auxiliary Space: O(1)
Example 2: Get the object with a max attribute value in a list of objects
Python3
from operator import attrgetter
class geeks:
def __init__(self, experience, salary):
self.experience = experience
self.salary = salary
object1 = geeks(7, 57050)
object2 = geeks(3, 98000)
object5 = geeks(6, 45000)
object4 = geeks(1, 89000)
object3 = geeks(5, 25000)
l = [object4, object3, object5, object2, object1]
max_attr = max(l, key=attrgetter('salary'))
print(max_attr.salary)
Time Complexity: O(n), where n is the total number of objects in the list.
Auxiliary Space: O(1)
Here is another approach to finding the object with the maximum attribute value in a list of objects in Python:
Python3
# Using a lambda function
class geeks:
def __init__(self, experience, salary):
self.experience = experience
self.salary = salary
object1 = geeks(2, 50000)
object2 = geeks(3, 60000)
object3 = geeks(1, 20000)
object4 = geeks(4, 75000)
l = [object4, object3, object2, object1]
max_attr = max(l, key=lambda x: x.salary)
print(max_attr.salary)
In this approach, we are using a lambda function to extract the salary attribute from each object and pass it to the key parameter of the max() function. The lambda function takes an argument x, which is an object from the list, and returns x.salary.
Time Complexity: O(n), where n is the total number of objects in the list.
Auxiliary Space: O(1)
Similar Reads
Python - Maximum value in record list as tuple attribute Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the max of a list as a tuple attribute. Letâs discuss certai
8 min read
Python | Find the sublist with maximum value in given nested list Given a list of list, the task is to find sublist with the maximum value in second column. Examples: Input : [['Paras', 90], ['Jain', 32], ['Geeks', 120], ['for', 338], ['Labs', 532]] Output :['Labs', 532] Input: [['Geek', 90], ['For', 32], ['Geeks', 120]] Output: ['Geeks', 120] Below are some tasks
4 min read
Python | Get first element with maximum value in list of tuples In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let's discuss certain ways in which this
4 min read
Python Program that displays the key of list value with maximum range Given a Dictionary with keys and values that are lists, the following program displays key of the value whose range in maximum. Range = Maximum number-Minimum number Input : test_dict = {"Gfg" : [6, 2, 4, 1], "is" : [4, 7, 3, 3, 8], "Best" : [1, 0, 9, 3]} Output : Best Explanation : 9 - 0 = 9, Maxim
5 min read
Python program to find Maximum value from dictionary whose key is present in the list Given a list with dictionary keys and a dictionary, extract maximum from dictionary values, whose key is present in list. Examples: Input : test_dict = {"Gfg": 4, "is" : 5, "best" : 10, "for" : 11, "geeks" : 3}, test_list = ["Gfg", "best", "geeks"] Output : 10 Explanation : Max value is 11, but not
6 min read
Python - Extract Item with Maximum Tuple Value Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract the item with maximum value of value tuple index. This kind of problem can have application in domains such as web development. Let's discuss certain ways in which this task can be performed. Input :
5 min read
Python - Key with Maximum element at Kth index in Dictionary Value List Given a dictionary with values as lists, the task is to write a Python program to get the key with the maximum element at the Kth index by comparing the elements of each list. Input : test_dict = {'Gfg' : [4, 6, 8, 2], 'is' : [1, 4, 5, 9], 'best' :[2, 3, 4, 10], 'for' :[4, 5, 2, 1], 'geeks' :[2, 10,
8 min read
Sort a Tuple of Custom Objects by Properties in Python We are given a tuple of custom objects and our task is to sort a tuple of custom objects by properties in Python and print the result. In this article, we will see how to sort a tuple of custom objects by properties in Python. Example: Input: CustomObject("Alice", 30), CustomObject("Bob", 25), Custo
3 min read
Python program to find the key of maximum value tuples in a dictionary Given a dictionary with values as tuples, the task is to write a python program to find the key of maximum value tuples. Examples: Input : test_dict = {'gfg' : ("a", 3), 'is' : ("c", 9), 'best' : ("k", 10), 'for' : ("p", 11), 'geeks' : ('m', 2)}Output : forExplanation : 11 is maximum value of tuple
6 min read
Python - Get maximum of Nth column from tuple list Sometimes, while working with Python lists, we can have a task in which we need to work with tuple list and get the maximum of its Nth index. This problem has application in web development domain while working with data information. Letâs discuss certain ways in which this task can be performed. Me
7 min read