Finding Max aggregate of list of Tuples

python get max of tuple list
python find max value in list of lists
python max tuple second element
python sort list of tuples descending
python get first element of tuple in list
find max and min in a list python without inbuilt function
how to find multiple max values in python
replace highest value of tuples in a list with the second highest value

Write a Python function maxaggregate(l) that takes a list of pairs of the form (name,score) as argument, where name is a string and score is an integer. Each pair is to be interpreted as the score of the named player. For instance, an input of the form [('Kohli',73),('Ashwin',33),('Kohli',7),('Pujara',122),('Ashwin',90)] represents two scores of 73 and 7 for Kohli, two scores of 33 and 90 for Ashwin and one score of 122 for Pujara. Your function should compute the players who have the highest aggregate score (aggegrate = total, so add up all scores for that name) and return the list of names of these players as a list, sorted in alphabetical order. If there is a single player, the list will contain a single name.

For instance:


should return:


because the aggregate score of Kolhi is 80, of Ashwin is 123 and of Pujara is 122, of which 123 is the highest.

def maxaggregate(l):
  d = {}
  for i in l:
    if i[0] in d:
      d[i[0]] += i[1]
      d[i[0]] = i[1]
  m = max(list(d.values()))
  x = []
  for i in d:
    if d[i] == m:
  return sorted(x)

I would probably use a defaultdict:

from collections import defaultdict
def maxaggregate(lst):
    # If a key is not in the dict it will
    # default the value for that key to 0 (the default int).
    d = defaultdict(int)

    # Sum up the scores
    for name, score in lst:
        d[name] += score

    # Return the max score by iterating
    # over the defaultdict's items and finding
    # the max score (the 2nd element 
    #     in the item is the score thus the x[1]).
    return max(d.items(), key=lambda x: x[1])

r = maxaggregate([('Kohli',73),('Ashwin',33),('Kohli',7),('Pujara',122),('Ashwin',90)]) 
print(r) # ('Ashwin', 123)

Python, Sometimes, data can come in the format of tuples and min and max The original list is : [(2, 3), (4, 7), (8, 11), (3, 6)] The min and max of index 1 : (2, 8) The min and Please Improve this article if you find anything incorrect by clicking on the  Sometimes, while working with Python records, we can have a problem in which we need to perform cross maximization of list of tuples. This kind of application is popular in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + zip ()

You can iterate over the list, create a dict to keep the scores and add up the scores by name. Then keeping note of the highest score you find with the associated name.

def maxaggregate(scores):
  agg_results = {}
  max_name = None
  max_val = None
  for name, score in scores:
    score += agg_results.get(name, 0)       # Add up the score
    agg_results[name] = score               # Save it in the dict
    if max_val is None or max_val < score:  # Compare scores
      max_val = score
      max_name = name
  return name


Returns: Ashwin

This solution only iterates over the list once.

Scala and 22, How do you find the maximum value of a tuple? print ("The original list is : " + str(test_list)) # using min () and max () # to get min and max in list of tuples. res1 = min(test_list) [0], max(test_list) [0] res2 = min(test_list) [1], max(test_list) [1] # printing result.

You can do the following, using a collections.Counter and itertools.takewhile:

from collections import Counter
from itertools import takewhile

def maxaggregate(l):
    c = Counter()
    for k, v in l:
        c[k] += v
    mc = c.most_common()
    max_count = mc[0][1]
    return sorted(name for name, _ in takewhile(lambda i: i[1] == max_count, mc))

How to sort a list of tuples by the second value in Python, What is the maximum number of elements in a tuple Scala? Method #1 : Using max () + operator.itemgetter () We can get the maximum of corresponding tuple index from a list using the key itemgetter index provided and then mention the index information required using index specification at the end. filter_none. edit.

Python, How do you find the max value in a list in Python? Finding Min and Max for a set of Tuple. Close. 3. Posted by. u/suri8585. 2 years ago. Archived. Finding Min and Max for a set of Tuple. I've tried to find min and max

Introduction to Database Systems, Python | Get first element with maximum value in list of tuples. Method #1 : Using max() + operator.itemgetter() We can get the maximum of corresponding tuple index from a list using the key itemgetter index provided and then mention the index information required using index specification at the end. Method #2 : Using There are multiple ways we can find the tuples containing the given element from a list of tuples. Let’s see some of Pythonic ways to do this task. Method #2: Using list comprehension. It works only when there are fixed number of elements in every list. For example 2 elements in below code.

Advances in Spatial and Temporal Databases: 15th International , Now, take another example of Online Book database, 'retrieve the list of all authors In this example, the relation R1^-R2 is the set of values that forms a tuple with Similarly, aggregate functions, namely, AVG, MAX, and MIN are used to find  Python List max() Method - Python list method max returns the elements from the list with maximum value.

  • It's a easy task but before giving you the answer, I wanted to give you hint. Try to iterate through list and make dictionary which has unique keys to save/add/update the score of each player. At the end you have the dictionary with player name and score without duplication.
  • @Error-SyntacticalRemorse I upvoted yours, nice solution with defaultdict, I always forget its existence haha. About your comment, by doing not max_val it wouldn't consider an score 0 and there is no restriction about this. What if we have [("Carl", 0)] as input, it should return Carl.
  • Yeah I realized that and deleted my comment but you noticed to quick. Well done.