Python – adding elements to multiple arrays

Feedback

Question:

If I intended to conduct a test similar to Levene’s test for
equal variances
using scipy stats, where two outputs (the test statistic and p-value) are generated for all the data in a dictionary, how can I add the outputs for each test to two separate lists? I attempted the following code:

test_stat[]
p_value[]
for i in range(0, n_data):
    for j in range(1, n_name):
        test_stat[i], p_value[i] = scipy.stats.levene(data[i][name[j-1]],
                                                      data[i][name[j]],
                                                      center='median')

However, this approach is clearly not the right one, as I consistently encounter an

IndexError

error due to the list assignment index being out of range.

I would greatly appreciate any suggestions. Thank you!


Solution:

It is not necessary for everything to be in one line; this alternative approach should be effective.

test_stats = []
p_values = []
for i in range(0, n_data):
    for j in range(1, n_name):
        test_stat, p_value = scipy.stats.levene(data[i][name[j-1]], 
                                                data[i][name[j]], 
                                                center='median')
        test_stats.append(test_stat)
        p_values.append(p_value)

Adding

n_data * n_name

rows is, of course, implied.

Frequently Asked Questions