Average of list in dictionary python

Python List Average Median

What’s the median of a Python list? Formally, the median is “the value separating the higher half from the lower half of a data sample” (wiki).

Average of list in dictionary python

How to calculate the median of a Python list?

  • Sort the list of elements using the sorted() built-in function in Python.
  • Calculate the index of the middle element (see graphic) by dividing the length of the list by 2 using integer division.
  • Return the middle element.

Together, you can simply get the median by executing the expression median = sorted(income)[len(income)//2].

Here’s the concrete code example:

income = [80000, 90000, 100000, 88000] average = sum(income) / len(income) median = sorted(income)[len(income)//2] print(average) # 89500.0 print(median) # 90000.0

Related tutorials:

  • Detailed tutorial how to sort a list in Python on this blog.

Python List Average Mean

The mean value is exactly the same as the average value: sum up all values in your sequence and divide by the length of the sequence. You can use either the calculation sum(list) / len(list) or you can import the statistics module and call mean(list).

Here are both examples:

lst = [1, 4, 2, 3] # method 1 average = sum(lst) / len(lst) print(average) # 2.5 # method 2 import statistics print(statistics.mean(lst)) # 2.5

Both methods are equivalent. The statistics module has some more interesting variations of the mean() method (source):

mean()Arithmetic mean (“average”) of data.
median()Median (middle value) of data.
median_low()Low median of data.
median_high()High median of data.
median_grouped()Median, or 50th percentile, of grouped data.
mode()Mode (most common value) of discrete data.

These are especially interesting if you have two median values and you want to decide which one to take.

Python List Average Standard Deviation

Standard deviation is defined as the deviation of the data values from the average (wiki). It’s used to measure the dispersion of a data set. You can calculate the standard deviation of the values in the list by using the statistics module:

import statistics as s lst = [1, 0, 4, 3] print(s.stdev(lst)) # 1.8257418583505538

Python List Average Min Max

In contrast to the average, there are Python built-in functions that calculate the minimum and maximum of a given list. The min(list) method calculates the minimum value and the max(list) method calculates the maximum value in a list.

Here’s an example of the minimum, maximum and average computations on a Python list:

import statistics as s lst = [1, 1, 2, 0] average = sum(lst) / len(lst) minimum = min(lst) maximum = max(lst) print(average) # 1.0 print(minimum) # 0 print(maximum) # 2

Need some explaining, i don't really get average and dictionaries..

I did “How is Everybody Doing?” - but I don’t really get the logic behind it O_o.

lloyd = { "name": "Lloyd", "homework": [90, 97, 75, 92], "quizzes": [88, 40, 94], "tests": [75, 90] } def average(some): return sum(some)/len(some) def get_class_average(student): total=0 for x in student: total+=get_average(x) return total/len(student)

So i use something like average(lloyd['homework']) and get the average score of Lloyds homework -that I understand: (sum of all homework elements)/by the number of elements. but could somebody explain how it works in

for x in student: total+=get_average(x) return total/len(student)

how does it take every students ‘name’, ‘homework’ etc (students=[lloyd, alice, tyler] get_class_average(students)) and make a single average score (and what does it do with ‘name’)? just don’t get it.. can somebody maybe explain it step by step?

Python – Dictionary Values Mean

Given a dictionary, find mean of all the values present.

Input : test_dict = {“Gfg” : 4, “is” : 4, “Best” : 4, “for” : 4, “Geeks” : 4}
Output : 4.0
Explanation : (4 + 4 + 4 + 4 + 4) / 4 = 4.0, hence mean.

Input : test_dict = {“Gfg” : 5, “is” : 10, “Best” : 15}
Output : 10.0
Explanation : Mean of these is 10.0

Method #1 : Using loop + len()

This is brute way in which this task can be performed. In this, we loop through each value and perform summation and then the result is divided by total keys extracted using len().






# Python3 code to demonstrate working of
# Dictionary Values Mean
# Using loop + len()
# initializing dictionary
test_dict = {"Gfg" : 4, "is" : 7, "Best" : 8, "for" : 6, "Geeks" : 10}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# loop to sum all values
res = 0
for val in test_dict.values():
res += val
# using len() to get total keys for mean computation
res = res / len(test_dict)
# printing result
print("The computed mean : " + str(res))
Output The original dictionary is : {'Gfg': 4, 'is': 7, 'Best': 8, 'for': 6, 'Geeks': 10} The computed mean : 7.0

Method #2 : Using sum() + len() + values()

The combination of above functions can be used to solve this problem. In this, we perform summation using sum() and size() of total keys computed using len().




# Python3 code to demonstrate working of
# Dictionary Values Mean
# Using sum() + len() + values()
# initializing dictionary
test_dict = {"Gfg" : 4, "is" : 7, "Best" : 8, "for" : 6, "Geeks" : 10}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# values extracted using values()
# one-liner solution to problem.
res = sum(test_dict.values()) / len(test_dict)
# printing result
print("The computed mean : " + str(res))
Output The original dictionary is : {'Gfg': 4, 'is': 7, 'Best': 8, 'for': 6, 'Geeks': 10} The computed mean : 7.0

Average of list in dictionary python




Article Tags :
Python
Python Programs
Python dictionary-programs

dict.values() Syntax:

In python, the dict class provides a member function to fetch all values from dictionary i.e.

dict.values()

Parameter:

      • None

Return Value:

Advertisements
      • It returns a sequence containing the view of all values from the dictionary. If any value gets modified in the dictionary then it will be reflected in sequence too, because that is just a view of values.

Let’s understand with some examples,

Examples of dict.values() in python