Search code examples
pythonstringscipyevalnested-lists

How to break a nested list and obtain the items separated with a comma?


I have a nested list called model and I want to use its items as the inputs of

scipy.stats.kruskal()     

That is, if my nested list has n=5 lists inside it, I want to obtain

scipy.stats.kruskal(model[0],model[1],model[2],model[3],model[4])

However n changes depending on the application, and sometimes, instead of 5, I may have 3 and etc.

My initial idea was

    s=''
    for i in range(n):
        a='model_errors[{}],'.format(i)
        s+=a
    s=s[:-1]
    scipy.stats.kruskal(eval(s))

However eval() returns the initial nested list, and not the terms separated with a comma. I've been trying many different things, but none of them were successful.


Solution

  • You can use *, it can pass a list to the function, and ** for keyword arguments like dictionaries. So try scipy.stats.kruskal(*model)