Python - Use List as Function Parameters

Python - use list as function parameters

You can do this using the splat operator:

some_func(*params)

This causes the function to receive each list item as a separate parameter. There's a description here: http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists

Getting list of parameter names inside python function

Well we don't actually need inspect here.

>>> func = lambda x, y: (x, y)
>>>
>>> func.__code__.co_argcount
2
>>> func.__code__.co_varnames
('x', 'y')
>>>
>>> def func2(x,y=3):
... print(func2.__code__.co_varnames)
... pass # Other things
...
>>> func2(3,3)
('x', 'y')
>>>
>>> func2.__defaults__
(3,)

For Python 2.5 and older, use func_code instead of __code__, and func_defaults instead of __defaults__.

Can you store functions with parameters in a list and call them later in Python?

It sounds like you want to store the functions pre-bound with parameters. You can use functools.partial to give you a function with some or all of the parameters already defined:

from functools import partial

def f(text):
print(text)

mylist = [partial(f, 'yes'), partial(f,'no')]

mylist[0]()
# yes

mylist[1]()
# no

Python function parameters inside a list of lists

Is this what you want:

def func(*vargs):
for a in vargs:
print a

func(*[1,2,3])
1
2

i.e.)

def func(*vargs):
for a in vargs:
print a

func(*main_list)

Edit

a,g,e=10,40,50

def func(a,b,c):
print a,b,c

func(*[a,g,e])
10 40 50

3

How to pass all element of a list as parameter of a function without passing the list as argument

It looks like what you do is the correct method.

Example:

def testB(argA, argB):
print(f'{argA=}')
print(f'{argB=}')

def testA(argA, argB, argC):
print(f'{argA=}')
print(f'{argB=}')
print(f'{argC=}')

def funcExecuter(func, nbArgs, *argv):
return func(*argv)

print('test1')
funcExecuter(testA, 3, 'a', 'b', 'c')
print('test2')
funcExecuter(testB, 2, 'a', 'b')

output:

test1
argA='a'
argB='b'
argC='c'
test2
argA='a'
argB='b'

ensuring the correct number of parameters

If you want to truncate or pad the parameters:

def testA(argA, argB, argC):
print(f'{argA=}')
print(f'{argB=}')
print(f'{argC=}')

def funcExecuter(func, nbArgs, *argv):
return func(*(list(argv[:nbArgs])+[None]*(nbArgs-len(argv))))

print('test1')
funcExecuter(testA, 3, 'a', 'b', 'c')
print('test2')
funcExecuter(testA, 3, 'a', 'b', 'c', 'd')
print('test3')
funcExecuter(testA, 3, 'a', 'b')

output:

test1
argA='a'
argB='b'
argC='c'
test2
argA='a'
argB='b'
argC='c'
test3
argA='a'
argB='b'
argC=None

NB. this is a simple example here, of course you can have a more complex check

Is it possible to use the elements of two different lists as function parameters in Python?

You can use zip to combine two lists together.

Here is an example.

list_of_ids = [1,2,3]
list_of_names = ['bob', 'joe', 'frank']

def function(id, name):
return f'{id} {name}'

for id,name in zip(list_of_ids,list_of_names):
print(function(id,name))

#1 bob
#2 joe
#3 frank

zip creates an 'zip' object that is iterable tubles of the two lists/tuples passed to it.

Passing a list of parameters into a Python function

some_list = ["some", "values", "in", "a", "list", ]
func(*some_list)

This is equivalent to:

func("some", "values", "in", "a", "list")

The fixed x param might warrant a thought:

func(5, *some_list)

... is equivalent to:

func(5, "some", "values", "in", "a", "list")

If you don't specify value for x (5 in the example above), then first value of some_list will get passed to func as x param.



Related Topics



Leave a reply



Submit