Reputation: 2566
i need to transfer the values in a dict (dict1 in the example below) to a function in an element by element manner (func1 in the example below, the function can not be changed)
def func1(input1,input2,input3):
print input1
print input2
print input3
dict1={"a":1,"b":2,"c":3}
keys=["a","b","c"]
func1(dict1["a"],dict1["b"],dict1["c"])
How can i improve the last line with the help of array keys? i have tried
func1([dict[key] for key in keys])
and
func1(dict[key] for key in keys)
Upvotes: 0
Views: 70
Reputation: 53029
Since you were asking for improvements. You didn't leave much room for them but some might say that not creating a throw-away list might count as one:
func1(*(dict1[key] for key in keys))
Obviously, as improvements go this one is very minor.
Upvotes: 1
Reputation: 33714
Your func1([dict[key1] for key in keys])
almost worked, you just need to unpack it before sending it to the function using *
func1(*[dict1[key] for key in keys])
Upvotes: 5