Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order dictionary index in python

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.

Accessing dictionary value by index in python

Here is my code and output.

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])

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.

TypeError: 'odict_values' object does not support indexing

KeyError: 1

TypeError: 'odict_keys' object does not support indexing

like image 249
Vishy Avatar asked Sep 02 '16 05:09

Vishy


People also ask

Can you order dictionaries in Python?

It is not possible to sort a dictionary, only to get a representation of a dictionary that is sorted. Dictionaries are inherently orderless, but other types, such as lists and tuples, are not. So you need an ordered data type to represent sorted values, which will be a list—probably a list of tuples.

How do you organize a dictionary in Python?

To sort a dictionary by value in Python you can use the sorted() function. Python's sorted() function can be used to sort dictionaries by key, which allows for a custom sorting method. sorted() takes three arguments: object, key, and reverse. Dictionaries are unordered data structures.

What is the order of keys in Python dictionary?

Python 3.6 (CPython) As of Python 3.6, for the CPython implementation of Python, dictionaries maintain insertion order by default. This is considered an implementation detail though; you should still use collections. OrderedDict if you want insertion ordering that's guaranteed across other implementations of Python.


2 Answers

In Python 3, dictionaries (including OrderedDict) return "view" objects from their keys() and values() methods. Those are iterable, but don't support indexing. The answer you linked appears to have been written for Python 2, where keys() and values() returned lists.

There are a few ways you could make the code work in Python 3. One simple (but perhaps slow) option would be to pass the view object to list() and then index it:

print(list(line_1.values())[1])

Another option would be to use itertools.islice to iterate over the view object to the desired index:

import itertools

print(next(itertools.islice(line_1.values(), 1, 2)))

But all of these solutions are pretty ugly. It may be that a dictionary is not the best data structure for you to use in this situation. If your data was in a simple list, it would be trivial to lookup any item by index (but lookup up by key would be harder).

like image 116
Blckknght Avatar answered Oct 01 '22 00:10

Blckknght


TypeError: 'odict_values' object does not support indexing

You need to make it into a list first and then access it:

print(list(line_1.values())[1]) # or print([*line_1.values()][1])

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

KeyError: 1

The key 1 isn't in the ordered dictionary, hence the KeyError. Use a valid key like 'A1.

TypeError: 'odict_keys' object does not support indexing

As previously, make it into a list in order to index it.


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

like image 30
Dimitris Fasarakis Hilliard Avatar answered Oct 01 '22 01:10

Dimitris Fasarakis Hilliard