Amit Pal
Amit Pal

Reputation: 11062

Working with tuples in python?

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

Answers (3)

histrio
histrio

Reputation: 1215

Another way:

list_a, list_b, list_c = map(get_value, (1,2,3))

Upvotes: 2

eumiro
eumiro

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

Levon
Levon

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

Related Questions