Solution 1

# Import the library here (must be python 3.4+)!
try:
  from statistics import variance
except ImportError:
  def variance(a_list):
    mean = sum(a_list) / float(len(a_list))
    return sum([(x - mean) ** 2 for x in a_list]) / float((len(a_list) - 1))

def get_variance_from_list(a_list):
    return variance(a_list)

Get Variance from List

Perhaps you need to calculate some statistics on data you have.

You COULD write your own code to do this. But you are a strong, confident, successful programmer with a busy schedule and ain't nobody got time for dat (unless you want the practice).

Import the statistics library and write a function get_variance_from_list that receives the list a_list and returns the variance for the numbers in that list using a method from the library.

Note: This particular library only works in python versions 3.4 and greater. Make sure when you run this code you have it set to Python 3 in the platform (button in top right on Learn)!

Examples:

get_variance_from_list([1, 3, 8, 4, 22]) # 71.3

Test Cases

test get variance - Run Test

def test_get_variance():
    assert get_variance_from_list([1, 3, 8, 4, 22]) == 71.3
# Import the library here (must be python 3.4+)! def get_variance_from_list(a_list): pass