Python Program to Find Variance of a List
mean = sum(lst) / len(lst), then compute variance as variance = sum((x - mean) ** 2 for x in lst) / len(lst).Examples
How to Think About It
Algorithm
Code
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))
Dry Run
Let's trace the list [2, 4, 6, 8] through the code
Calculate mean
mean = (2 + 4 + 6 + 8) / 4 = 20 / 4 = 5.0
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
Sum squared differences
9 + 1 + 1 + 9 = 20
Calculate variance
variance = 20 / 4 = 5.0
| Number | Difference from Mean | Squared Difference |
|---|---|---|
| 2 | 2 - 5 = -3 | 9 |
| 4 | 4 - 5 = -1 | 1 |
| 6 | 6 - 5 = 1 | 1 |
| 8 | 8 - 5 = 3 | 9 |
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
import statistics numbers = [2, 4, 6, 8] variance = statistics.pvariance(numbers) print(variance)
import numpy as np numbers = [2, 4, 6, 8] variance = np.var(numbers) print(variance)
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Manual calculation | O(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 |
statistics.pvariance() for a quick and reliable variance calculation.