This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Support index access on OrderedDict views (e.g. o.keys()[7])
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: cool-RR, methane, rhettinger, ulope
Priority: low Keywords:

Created on 2014-07-14 09:15 by cool-RR, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg223006 - (view) Author: Ram Rachum (cool-RR) * Date: 2014-07-14 09:15
Implement `__getitem__` on `OrdredDict.keys`, `OrdredDict.values` and `OrdredDict.items`, so the following code snippet wouldn't error:

    >>> from collections import OrderedDict
    >>> o = OrderedDict(((1, 2), (3, 4), (5, 6)))
    >>> o
    OrderedDict([(1, 2), (3, 4), (5, 6)])
    >>> o.keys()
    KeysView(OrderedDict([(1, 2), (3, 4), (5, 6)]))
    >>> o.keys()[0]
    Traceback (most recent call last):
      File "<string>", line 1, in <fragment>
    builtins.TypeError: 'KeysView' object does not support indexing
    >>> o.values()[0]
    Traceback (most recent call last):
      File "<string>", line 1, in <fragment>
    builtins.TypeError: 'ValuesView' object does not support indexing
    >>> o.items()[0]
    Traceback (most recent call last):
      File "<string>", line 1, in <fragment>
    builtins.TypeError: 'ItemsView' object does not support indexing
msg223008 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-07-14 10:12
I'm not sure this would make sense given that the ordered dict itself isn't indexable, given that keys/values/items on regular dicts aren't indexable, and given that keys/items views are set-like rather than sequence-like.

The one obvious way to get sequence behavior is to build a list:

   s = list(od)
   s = list(od.values())
   s = list(od.items())
msg223085 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-07-15 04:28
Closing this.  The premise is flawed.  Guido designed mapping views to be a pass-through to the underlying mapping.  As such, they would only have sequence behavior if the underlying mapping had sequence behavior.  

The implementation of both regular dicts and OrderedDicts don't support reasonable ways to index by position.   Guido essentially decided on the current behavior this when he decided to do away the Python's dict.items() being a list and replacing it with a mapping view.  See PEP 3016 for his rationale and the related discussion.
msg282628 - (view) Author: Ulrich Petri (ulope) * Date: 2016-12-07 15:51
Should this maybe reconsidered now that dicts are ordered by default?

Having to explain why list is needed in list(some_ordered_dict.values())[0] is  a constant thorn in my side when dealing with people new to Python.
msg282701 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2016-12-08 09:53
While dict is ordered, it doesn't support O(1) random access by index.
History
Date User Action Args
2022-04-11 14:58:05adminsetgithub: 66177
2016-12-08 09:53:03methanesetnosy: + methane
messages: + msg282701
2016-12-07 15:51:54ulopesetnosy: + ulope
messages: + msg282628
2014-07-15 04:28:01rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg223085
2014-07-14 10:12:32rhettingersetpriority: normal -> low

nosy: + rhettinger
messages: + msg223008

assignee: rhettinger
type: enhancement
2014-07-14 09:15:03cool-RRcreate