Using multiple lists as arguments for python function

İ know it is a little easy but i want you to ask a question
i have 2 lists and i want to make a calculation one by one each other in a function. but i got error. could you plese tell me how could i approach this problem. thank you

def calcg1(L,T):
    pi42=4*(math.pi ** 2)
    g=pi42*L/(T**2)
    return g

T=[1.462,1.434,1.227,1.192,1.256,1.616,2.197]
L=[35,30,25,20,15,10,5]

results=calcg1(T,L)

Hi Obyilmaz,

Your post is not clear to me about what you are actually trying to do

and what result you expected. It would help a lot if you show the result

you are hoping to get.

What error did you get? I hope it is not a secret wink

I’m going to guess that what you want to do is take the first number

from T and the first number from L, and calculate a result from them.

Then take the second number from T and the second number from L, and

calculate another result, and so on. Is that what you want?

If it is, you can do it like this:


results = []

for x, y in zip(L, T):

    results.append(calcg1(x, y))



The zip() function goes through the two lists, L and T, and collects

one item from each. Then those two values get assigned to x and y.

Then it calls your function calcg1() with the x and y, and appends the

result in the list results.

We can do the same thing in one line like this:

results = [calcg1(x, y) for x,y in zip(L, T)]

Does that help?

1 Like

exactly … i found similar solution on my own like this.

def calcg2(L1,T1):
    result=[]
    for L,T in zip(L1,T1) :
        pi42=4*(math.pi**2)
        g=pi42*L/(T**2)
        result.append(g)
    return result

i thougt it is the same but ur version is more appropriate. thank you

It looks like you are trying to do a vector add operation. numpy will do this