I have a dictionary such as:
d = {'a':'a-val', 'b':'b-val', 'c':'c-long-val'*1000}
And I need to repeatedly access d['c']
as in:
print('value of c is', d['c'])
x_queue.put(d['c'])
some_function(d['c'])
But I'm wondering if it would be faster to assign d['c']
to a variable and use it each time:
c_value = d['c']` # is this assignment "worth it"?
print('value of c is', c_value)
x_queue.put(c_value)
some_function(c_value)
My hunch is it may depend on
- number of elements in
d
(finding key is more costly with biggerd
) - size of
d['c']
(assignment is more costly with biggerd['c']
)
But I'm really not sure if one of those options (or another?) is faster or more pythonic.