Hi people of Stackoverflow,
I have a function which calculates a list and I want to return each element of the list individually, like so (the function receiving this return is designed to handle an undefined number of parameters):
def foo():
my_list = [1,2,3,4]
return 1,2,3,4
The number of elements in the list is undefined so I can't do:
return my_list[0], my_list[1], ...
I think there must be an easy solution to this but I can't seem to wrap my head around it.
Any suggestions? Thanks!
EDIT: Thanks for your answers, so here is the extension to my problem, which is really my problem:
def foo():
my_list = [1,2,3,4]
return "any_other_value", my_list
So, what I want is that when I call foo(), bar = foo(), bar[0] = "any_other_value", bar[1] = 1, bar[2]=3, bar[3] = 4. And my_list is of undetermined length (here it has only 4 elements).
解决方案
You are overcomplicating things. Just do what you were doing as (I suppose you could convert it to how you are showing it with tuple(my_list) but it is unnecessary):
return my_list
This is all you need to do because when you call that function that takes an undefined number of parameters, you can just call it as such:
function_with_undefined_number_of_params(*foo())
The * unpacks the returned list into separate arguments.