0
0
PythonProgramBeginner · 2 min read

Python Program to Find Variance of a List

To find the variance of a list in Python, calculate the mean with mean = sum(lst) / len(lst), then compute variance as variance = sum((x - mean) ** 2 for x in lst) / len(lst).
📋

Examples

Input[2, 4, 6, 8]
Output5.0
Input[1, 1, 1, 1]
Output0.0
Input[10, 20, 30]
Output66.66666666666667
🧠

How to Think About It

To find variance, first find the average (mean) of the numbers. Then, for each number, find how far it is from the mean by subtracting the mean and squaring the result. Finally, average these squared differences to get the variance.
📐

Algorithm

1
Get the list of numbers.
2
Calculate the mean by adding all numbers and dividing by the count.
3
For each number, subtract the mean and square the result.
4
Sum all squared differences.
5
Divide the sum by the number of elements to get the variance.
6
Return the variance.
💻

Code

python
def find_variance(lst):
    mean = sum(lst) / len(lst)
    variance = sum((x - mean) ** 2 for x in lst) / len(lst)
    return variance

numbers = [2, 4, 6, 8]
print(find_variance(numbers))
Output
5.0
🔍

Dry Run

Let's trace the list [2, 4, 6, 8] through the code

1

Calculate mean

mean = (2 + 4 + 6 + 8) / 4 = 20 / 4 = 5.0

2

Calculate squared differences

For each x: (x - mean)^2 = (2-5)^2=9, (4-5)^2=1, (6-5)^2=1, (8-5)^2=9

3

Sum squared differences

9 + 1 + 1 + 9 = 20

4

Calculate variance

variance = 20 / 4 = 5.0

NumberDifference from MeanSquared Difference
22 - 5 = -39
44 - 5 = -11
66 - 5 = 11
88 - 5 = 39
💡

Why This Works

Step 1: Calculate the mean

The mean is the average value of the list, found by adding all numbers and dividing by how many there are using sum(lst) / len(lst).

Step 2: Find squared differences

For each number, subtract the mean and square the result to measure how far each number is from the average.

Step 3: Calculate variance

Sum all squared differences and divide by the number of elements to get the average squared distance, which is the variance.

🔄

Alternative Approaches

Using statistics module
python
import statistics
numbers = [2, 4, 6, 8]
variance = statistics.pvariance(numbers)
print(variance)
This method is simpler and uses built-in functions but requires Python 3.4+.
Using numpy library
python
import numpy as np
numbers = [2, 4, 6, 8]
variance = np.var(numbers)
print(variance)
Numpy is efficient for large data but adds an external dependency.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through the list twice: once to calculate the mean and once to calculate squared differences, resulting in O(n) time.

Space Complexity

Only a few variables are used to store sums and the mean, so space complexity is O(1).

Which Approach is Fastest?

Using built-in modules like statistics or numpy is optimized and faster for large data, but manual calculation is fine for small lists.

ApproachTimeSpaceBest For
Manual calculationO(n)O(1)Small lists, learning purpose
statistics.pvariance()O(n)O(1)Simple, built-in, reliable
numpy.var()O(n)O(1)Large data, scientific computing
💡
Use Python's built-in statistics.pvariance() for a quick and reliable variance calculation.
⚠️
Beginners often forget to square the differences or divide by the number of elements, leading to incorrect variance.