I have the following function logic.
def funct(arg):
...
return result_list
param_list = ['param1','param2']
for my_arg in param_list
my_results = funct(my_arg)
How do I get the full list of results for running the function on both parameters? Is there a better way to organize this?
CodePudding user response:
Make my_results
a list, and extend it on each call.
my_results = []
for my_arg in param_list
my_results.extend(funct(my_arg))
CodePudding user response:
If you want a single message, create a dictionary mapping the inputs to the outputs and pack it all at once
Beware very large dicts can be troublesome and you may want to truncate it, base64-encode it, or do other work to reduce the result size
results = {}
for my_arg in param_list
results[my_arg] = funct(my_arg)
logger.info(f"results mapping: {results}")
CodePudding user response:
This is a rare case where you may actually want generator delegation, which could make your functions much more efficient (as they can avoid creating intermediate lists, which are only discarded)
def funct(input_value):
for result in some_source(input_value):
yield result
def funct_wrapper(input_values):
for input_value in input_values:
yield from funct(input_value)
Collect and use the results, adding them to some collection to display later
CodePudding user response:
Make my_results as list append or extend it on each iterations ,
example enter code here`mentioned below:
def funct(arg):
....
return result_list
my_results=[]
param_list = ['param1','param2']
for my_arg in param_list:
my_results.append(funct(my_arg))
# print(my_results)