How can I calculate the mean and standard deviation for each index across multiple lists in Python?

How can I calculate the mean and standard deviation for each index across multiple lists in Python?

For example, given the following lists:

A_rank = [0.8, 0.4, 1.2, 3.7, 2.6, 5.8]
B_rank = [0.1, 2.8, 3.7, 2.6, 5.0, 3.4]
C_rank = [1.2, 3.4, 0.5, 0.1, 2.5, 6.1]

I want to calculate the mean and standard deviation of the first index across all lists (A_rank[0], B_rank[0], C_rank[0]), then the second index (A_rank[1], B_rank[1], C_rank[1]), and so on.

How can I efficiently compute this in Python while focusing on standard deviation with Python lists?

The zip() function is an effective way to group corresponding elements of multiple lists, and the statistics module provides built-in methods for mean and standard deviation calculations.

Example:

import statistics

A_rank = [0.8, 0.4, 1.2, 3.7, 2.6, 5.8]
B_rank = [0.1, 2.8, 3.7, 2.6, 5.0, 3.4]
C_rank = [1.2, 3.4, 0.5, 0.1, 2.5, 6.1]

# Group corresponding elements and calculate mean and standard deviation
for group in zip(A_rank, B_rank, C_rank):
mean = statistics.mean(group)
std_dev = statistics.stdev(group)
print(f"Group: {group}, Mean: {mean}, Std Dev: {std_dev}")

NumPy is highly optimized for numerical calculations, making it ideal for tasks like finding the standard deviation in Python lists.

import numpy as np  

A_rank = [0.8, 0.4, 1.2, 3.7, 2.6, 5.8]  
B_rank = [0.1, 2.8, 3.7, 2.6, 5.0, 3.4]  
C_rank = [1.2, 3.4, 0.5, 0.1, 2.5, 6.1]  

data = np.array([A_rank, B_rank, C_rank])  
means = np.mean(data, axis=0)  
std_devs = np.std(data, axis=0)  

print("Means:", means)  
print("Standard Deviations:", std_devs)