0

I have a function run_model which takes in 5 arguments

run_model(lr_model,X_train_full,y_train_full,X_test_full,y_test_full)

can I call the function with one defined argument and a list

run_model(lr_model,[full_data])

full_data = (X_train_full,y_train_full,X_test_full,y_test_full)
unaied
  • 197
  • 11

2 Answers2

1

Yes, you can use the unpacking * feature of python. This operator unpacks the values of a list into separate values, so you can pass this into a function.

full_data = (X_train_full,y_train_full,X_test_full,y_test_full)
run_model(lr_model, *full_data)

This is equivalent to passing every value separately.

Adrià
  • 51
  • 4
0

You have a few options when declaring a function to accept many arguments

  • accept a wildcard list of *args
  • pass in the argument as a list foo(arg1, [arg2])

If you mean passing in all the args to a function set up to accept 5 (or any number), as @Paul M notes, you can unpack the arguments to a function with *

This also works for named arguments, but you might use **kwargs to accept and ** to unpack from a dictionary

>>> "{a} {c} {a}".format(**{'a':1, 'b':2, 'c': 3})
'1 3 1'

NOTE the names args and kwargs are by-convention, but choosing something else may cause confusion

ti7
  • 16,375
  • 6
  • 40
  • 68