Lets say I have the following code:
import collections
d = collections.OrderedDict()
d[\'foo\'] = \'python\'
d[\'bar\'] = \'spam\'
Is there
If you're dealing with fixed number of keys that you know in advance, use Python's inbuilt namedtuples instead. A possible use-case is when you want to store some constant data and access it throughout the program by both indexing and specifying keys.
import collections
ordered_keys = ['foo', 'bar']
D = collections.namedtuple('D', ordered_keys)
d = D(foo='python', bar='spam')
d[0] # result: python
d[1] # result: spam
d.foo # result: python
d.bar # result: spam
Or better:
getattr(d, 'foo') # result: python
getattr(d, 'bar') # result: spam