Cannot access sub-keys in OrderedDict
<p>I'm trying to define my ordinary dictionary as an OrderedDict, but I can't seem to access keys are the inner level.</p>

<pre><code>my_dict = \
{
    'key1':
    {
        'subkey1':value1,
        'subkey2':value2
    }
}

my_ordered_dict = OrderedDict\
([(
    'key1',
    (
        ('subkey1',value1),
        ('subkey2',value2)
    )
)])
</code></pre>

<p>I can access <code>['key1']</code> for both cases, but I cannot access <code>['key1']['subkey1']</code> for the ordered dict.</p>

<p>What am I doing wrong?</p>

<p>If you want the inner level to also be an <code>OrderedDict</code> you need to explicitly define it as one. Since argument order matters when constructing <code>OrderedDict</code>s, in order to preserve it, you need to pass the keys and values as a sequence of <code>key, value</code> pairs as shown in the <a href="https://docs.python.org/2/library/collections.html#ordereddict-examples-and-recipes" rel="nofollow"><em><code>OrderedDict</code> Examples and Recipes</em></a> section of the documentation.</p>

<p>Applying that to your example means you would need to do something like the following:</p>

<pre><code>from collections import OrderedDict

value1, value2 = 42, 69

my_ordered_dict = OrderedDict([
                    ('key1', OrderedDict([
                                ('subkey1', value1),
                                ('subkey2', value2)
                             ])
                    )
                  ])

print(my_ordered_dict['key1']['subkey1'])  # --&gt; 42
</code></pre>

python ordereddictionary