Reputation: 11062
I have three lists in python. Let's say :
list_a
list_b
list_c
Now I am calling a method which also return a list and save into the above lists. For example:
list_a = get_value(1)
list_b = get_value(2)
list_c = get_value(3)
I think the way I choose to call a same methods with different parameters are not a good option. Would it be possible for us to call these methods within one line?
I am beginner in Python, but what about the tuple option?
Upvotes: 0
Views: 990
Reputation: 213125
A list of lists?
ll = [get_value(x) for x in (1, 2, 3)]
or with your three lists:
list_a, list_b, list_c = [get_value(x) for x in (1, 2, 3)]
taking into account, that get_value
cannot be modified and returns just the single list.
Another possibility would be a dictionary.
params = {'a': 1, 'b': 2, 'c': 3}
result = {k:get_value(v) for k,v in params.iteritems()}
# result == {'a': get_value(1), 'b': get_value(2), 'c': get_value(3)}
Upvotes: 3
Reputation: 143172
If your function was set up correctly (ie modified to return the same number of lists as you send in parameters) you could have a call that looked like this:
list_a, list_b, list_c = get_value(1, 2, 3)
where get_value()
would return a tuple of 3 values.
Is that what you are asking about?
Upvotes: 3