Home > Mobile >  How do I pass a array/list to python if I know exactly how big it is going to be?
How do I pass a array/list to python if I know exactly how big it is going to be?

Time:11-02

I have this code and it works and I can continue on with this so this is an efficiency question. I want to pass an array into this function and I know each time it is going to be 3x1. This works but I wondering I could save computation time if I could specify the size. I tried saying def FUNC(p0=[3]): but it does not work.

def FUNC(p0=[]):

    print(p0[1])
    pass

FUNC([1,1,2])

CodePudding user response:

One way is just to use three distinct variables, and unpack the list while calling the function, but I'm not sure about the efficiency:

def fn(x,y,z):
    print(x,y,z)

fn(*[2,3,4])
2 3 4

But it's also certain that there is no any other way to limit the number of values that can be passed to the function.

CodePudding user response:

Python does not copy the list before passing it to the function. Instead it passes a reference to the entire array, which takes the same amount of time whether the list is of length 3 or length 1000000. So you cannot reduce the computation time, and you should not try.

Computers are fast. Unless your program is too slow, don't worry about computation speed.

  • Related