Order dictionary index in python
<p>I have created a order dictionary and could not get the index out of it.
I have gone through the below url but not working.</p>

<blockquote>
  <p><a href="http://stackoverflow.com/questions/15114843/accessing-dictionary-value-by-index-in-python">Accessing dictionary value by index in python</a></p>
</blockquote>

<p>Here is my code and output.</p>

<pre><code>line_1 = OrderedDict((('A1', "Miyapur"), ('A2', "JNTU College"), ('A3', "KPHB Colony"),
                ('A4', "Kukatpally"), ('A5', "Balanagar"), ('A6', "Moosapet"),
                ('A7', "Bharat Nagar"), ('A8', "Erragadda"), ('A9', "ESI Hospital"),
                ('A10', "S R Nagar"), ('X1', "Ameerpet"), ('A12', "Punjagutta"),
                ('A13', "Irrum Manzil"), ('A14', "Khairatabad"), ('A15', "Lakdikapul"),
                ('A16', "('Assembly"), ('A17', "Nampally"), ('A18', "Gandhi Bhavan"),
                ('A19', "Osmania Medical College"), ('X2', "MG Bus station"), ('A21', "Malakpet"),
                ('A22', "New Market"), ('A23', "Musarambagh"), ('A24', "Dilsukhnagar"),
                ('A25', "Chaitanyapuri"), ('A26', "Victoria Memorial"), ('A27', "L B Nagar")))

print(line_1.values()[1])
print(line_1[1])
print(line_1.keys()[1])
</code></pre>

<p>All the above options are not working as mentioned in the referenced link.
Any guidance is highly appreciated.
Here is the output for each print statement in the given order.</p>

<blockquote>
  <p>TypeError: 'odict_values' object does not support indexing</p>
  
  <p>KeyError: 1</p>
  
  <p>TypeError: 'odict_keys' object does not support indexing</p>
</blockquote>

<blockquote>
  <p>TypeError: 'odict_values' object does not support indexing</p>
</blockquote>

<p>You need to make it into a <code>list</code> first and then access it:</p>

<pre><code>print(list(line_1.values())[1]) # or print([*line_1.values()][1])
</code></pre>

<p>These <code>view</code> (new in Python 3) objects act more like sets which do not support indexing they .</p>

<blockquote>
  <p>KeyError: 1</p>
</blockquote>

<p>The key <code>1</code> isn't in the ordered dictionary, hence the <code>KeyError</code>. Use <em>a valid key</em> like <code>'A1</code>.</p>

<blockquote>
  <p>TypeError: 'odict_keys' object does not support indexing</p>
</blockquote>

<p>As previously, make it into a <code>list</code> in order to index it.</p>

<hr>

<p>Same applies for <code>line_1.items</code>, in order to index it you should first cast it to an object that supports indexing (<code>list</code>, <code>tuple</code>, custom etc)</p>

python python-3.x dictionary