title: Right way to initialize an OrderedDict using its constructor such that it retains order of initial data?
body: <p>What's the correct way to initialize an ordered dictionary (OD) so that it retains the order of initial data?</p>
  
  <pre><code>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])
  </code></pre>
  
  <p>Question: </p>
  
  <ul>
  <li><p>Will an <code>OrderedDict</code> 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 &amp; 3rd example above)?</p></li>
  <li><p>How does one go about verifying if <code>OrderedDict</code> actually maintains an order? Since a <code>dict</code> 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 <code>d = OrderedDict({'b':2, 'a':1})</code> I write <code>d = OrderedDict({'a':1, 'b':2})</code>, I can wrongly conclude that the order is preserved. In this case, I found out that a <code>dict</code> 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></li>
  </ul>
  
  <p>P.S. I'll just leave this here for <a href="https://docs.python.org/2.7/library/collections.html#collections.OrderedDict" rel="noreferrer">reference</a>: "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>P.P.S : Hopefully, in future, OrderedDict will preserve the order of kwargs also (example 1): <a href="http://bugs.python.org/issue16991" rel="noreferrer">http://bugs.python.org/issue16991</a></p>