collections.OrderedDict
keeps track of the order in which elements were added to it. This would work fine in a loop:
c = collections.OrderedDict()
for a,b in zip('abc', (1,2,3)):
c[a] = b
However, the expression OrderedDict(a=1,b=2,c=3)
creates an OrderedDict
by passing several keyword arguments to its constructor. In Python 2.7, the order of keyword arguments is not guaranteed. If you want that, you’ll have to move to Python 3.6, which implements PEP 468, Preserving the order of **kwargs in a function.
The
**kwargs
syntax in a function definition indicates that the interpreter should collect all keyword arguments that do not correspond to other named parameters. However, Python does not preserved the order in which those collected keyword arguments were passed to the function. In some contexts the order matters. This PEP dictates that the collected keyword arguments be exposed in the function body as an ordered mapping.