Python Forum
TypeError: 'dict_items' object does not support indexingw - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: TypeError: 'dict_items' object does not support indexingw (/thread-37134.html)



TypeError: 'dict_items' object does not support indexingw - Skaperen - May-04-2022

why does 'dict_items' not support indexing? this is the object type you get from dict.items(). shouldn't it interface like a tuple?


RE: TypeError: 'dict_items' object does not support indexingw - ndc85430 - May-04-2022

Presumably because the order of items isn't meaningful, in the same way it isn't meaningful for a dict.


RE: TypeError: 'dict_items' object does not support indexingw - buran - May-04-2022

check https://stackoverflow.com/a/52900459/4046632


RE: TypeError: 'dict_items' object does not support indexingw - Skaperen - May-06-2022

(May-04-2022, 05:10 PM)ndc85430 Wrote: Presumably because the order of items isn't meaningful, in the same way it isn't meaningful for a dict.
because of the lack of indexing, it can't behave like a tuple and be used by code that intentionally accesses it with indexing for one of a few reasons i have come across even though in the case of dict.items, none of those reasons were involved. i ended up coding an almost duplicate function just to handle it as an iterator. what was being done need to do reverse-order access some segments of the tuples it got, and it needed to handle it with indexing to keep the access in sync.

the order of items in dict.items values truly had no meaning. but the code needed to handle it that way to avoid the duplication so that other cases that needed to, could use indexing.