# Right way to initialize an OrderedDict using its constructor such that it retains order of initial data?

| | | | | | | | | | | | | | | | | |

What"s the correct way to initialize an ordered dictionary (OD) so that it retains the order of initial data?

``````from collections import OrderedDict

# Obviously wrong because regular dict loses order
d = OrderedDict({"b":2, "a":1})

# An OD is represented by a list of tuples, so would this work?
d = OrderedDict([("b",2), ("a", 1)])

# What about using a list comprehension, will "d" preserve the order of "l"
l = ["b", "a", "c", "aa"]
d = OrderedDict([(i,i) for i in l])
``````

Question:

• Will an `OrderedDict` preserve the order of a list of tuples, or tuple of tuples or tuple of lists or list of lists etc. passed at the time of initialization (2nd & 3rd example above)?

• How does one go about verifying if `OrderedDict` actually maintains an order? Since a `dict` has an unpredictable order, what if my test vectors luckily have the same initial order as the unpredictable order of a dict? For example, if instead of `d = OrderedDict({"b":2, "a":1})` I write `d = OrderedDict({"a":1, "b":2})`, I can wrongly conclude that the order is preserved. In this case, I found out that a `dict` is ordered alphabetically, but that may not be always true. What"s a reliable way to use a counterexample to verify whether a data structure preserves order or not, short of trying test vectors repeatedly until one breaks?

P.S. I"ll just leave this here for reference: "The OrderedDict constructor and update() method both accept keyword arguments, but their order is lost because Python‚Äôs function call semantics pass-in keyword arguments using a regular unordered dictionary"

P.P.S : Hopefully, in future, OrderedDict will preserve the order of kwargs also (example 1): http://bugs.python.org/issue16991

## Shop

Best Python online courses for 2022

\$FREE

Best laptop for Excel

\$

Best laptop for Solidworks

\$399+

Best laptop for Roblox

\$399+

Best computer for crypto mining

\$499+

Best laptop for Sims 4

\$

Best laptop for Zoom

\$499

Best laptop for Minecraft

\$590

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries