Search code examples
pythonlistdictionarylist-comprehensiondictionary-comprehension

Calculate median of dictionary values inside list


I'm looking to calculate the median of "score" (a dictionary value) inside a list.

my_dict = {"John": [{"class": "math", "score": 100, "year": 2014}, {"class": "english", "score": 85, "year": 2015}, {"class": "science", "score": 90, "year": 2015}], 
"Timmy": [{"class": "math", "score": 87, "year": 2014}, {"class": "english", "score": 91, "year": 2015}], 
"Sally":[{"class": "math", "score": 95, "year": 2014}]}

The output would look like:

new_dict = {"John": 90, "Timmy": 89, "Sally": 95}

I figured I need to sort my_dict based on score and then calculate the median value. Can't quite figure out either step without using an exterior package.

Any help would be greatly appreciated! New to Python.


Solution

  • You can use the median from statistics.

    from statistics import median
    
    my_dict = {"John": [{"class": "math", "score": 100, "year": 2014}, {"class": "english", "score": 85, "year": 2015}, {"class": "science", "score": 90, "year": 2015}], 
    "Timmy": [{"class": "math", "score": 87, "year": 2014}, {"class": "english", "score": 91, "year": 2015}], 
    "Sally":[{"class": "math", "score": 95, "year": 2014}]}
    
    new_dict = {}
    
    for k, v in my_dict.items():
      m = []
      for l in v:
        m.append(l["score"])
      new_dict[k] = median(m)
    
    print(new_dict)
    

    If you don't want to use a package and write your own function, you can call this:

    def median(lst):
        n = len(lst)
        s = sorted(lst)
        return (sum(s[n//2-1:n//2+1])/2.0, s[n//2])[n % 2] if n else None