1

I have a given list and a custom function with some parameters. The list contains the parameters in string form and i want to change these parameters to True if list contains else remain false in function.

Here is example:

list_params = ['remove_digits','remove_stopwords','clean_data']

custom_function(remove_digits =False, clean_data =False, remove_stopwords = False, text_lemmatization =False)

Here all parameters are false first but as soon as list contains these parameters, select them to True in function else remains false. I want all the parameters to True at once if present in list.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
martian_rover
  • 339
  • 6
  • 15

2 Answers2

2

Assuming your function definition is something like:

def custom_function(remove_digits=False, clean_data=False, remove_stopwords=False, text_lemmatization=False):

So you want to "turn on" the arguments from the list. You can do this by converting the list to a kwargs dict and unpack it to the function call:

list_params = ['remove_digits','remove_stopwords','clean_data']
dict_params = {param: True for param in list_params}

custom_function(**dict_params)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • can you explain ** doing in function? – martian_rover Jan 08 '21 at 16:26
  • I added some links which explain it extensively more than I am able to. But in the most basic way, if you have `kwargs = {'x': 1, 'y': 2}` then doing `foo(**kwargs)` *unpacks* that dict and it's equivalent to doing `foo(x=1, y=2)` – Tomerikoo Jan 08 '21 at 16:27
1

The best would probably to change the signature of custom_function to receive a list of parameters. If you cannot change it, you can still call your function like this:

custom_function(remove_digits in list_params, clean_data in list_params, remove_stopwords in list_params, text_lemmatization in list_params)

Otherwise, if you have the list of the parameters expected by the function, for example the list params = ['remove_digits', 'clean_data', 'remove_stopwords', 'text_lemmatization'] (which you can get with inspect.signature), you can write args = [x in list_params for x in params], and call custom_function as custom_function(*args):

list_params = ['remove_digits','remove_stopwords','clean_data']

import inspect
params = list(inspect.signature(custom_function).parameters)
# params = ['remove_digits', 'clean_data', 'remove_stopwords', 'text_lemmatization']
args = [x in list_params for x in params]
custom_function(*args)
Thibault D.
  • 201
  • 1
  • 4
  • will this make parameters in function to True/False? and whatif i don't know what are the parameters in list? – martian_rover Jan 08 '21 at 15:27
  • `x in l` indeed returns a boolean, true if x is an element of the list l, and false otherwise. – Thibault D. Jan 08 '21 at 15:32
  • If you don't know the parameters expected, you can still use something like [inspect signature](https://docs.python.org/3/library/inspect.html#inspect.signature) and use the * [operator](https://stackoverflow.com/questions/3941517/converting-list-to-args-when-calling-function) – Thibault D. Jan 08 '21 at 15:34
  • 1
    When you have the list of the parameters expected by the function, for example the list `params = ['remove_digits', 'clean_data', 'remove_stopwords', 'text_lemmatization']`, you can write `args = [x in list_params for x in params]`, and call custom_function as `custom_function(*args)` – Thibault D. Jan 08 '21 at 15:36
  • I think this will work, can you pls add this to main answer so that i can accept it...thanks – martian_rover Jan 08 '21 at 15:37
  • Done! (I also added details on how to do it with inspect.signature so that it works for any function) – Thibault D. Jan 08 '21 at 16:25