Python Forum

Full Version: TypeError: 'dict_items' object does not support indexingw
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
why does 'dict_items' not support indexing? this is the object type you get from dict.items(). shouldn't it interface like a tuple?
Presumably because the order of items isn't meaningful, in the same way it isn't meaningful for a dict.
(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.