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)
    return g



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):
    for L,T in zip(L1,T1) :
    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