You have to check the C code to be sure, but IIRC the latest dict
implementation has a dense array of the values in insert order, and the
hash table (which has gaps) contains indexes into the values array. So you
could easily index into the values array (which I believe also has the
keys) in O(1) time.

Though what happens to the dense array when a key is deleted? It must leave
a gap there too. So, never mind, you’d have to walk through the array
counting items but not gaps, and that’s O(n). Which explains why we don’t
have such an API. But please check the C code!

—Guido

On Sun, Oct 10, 2021 at 07:18 Alex Waygood <alex.wayg...@gmail.com> wrote:

> Should `dict.items()` be indexable now that dicts are ordered? I say yes.
> Why shouldn't it?
>
>
> Would there be a way to ensure that this had the same time complexity as
> indexing of sequences? If "yes", I would support this — I think it would be
> useful in some situations, and it would be more efficient than existing
> mechanisms to obtain the *n*th key from a dictionary. If (as I presume),
> the answer is "no", then I would not support this — I think it would give
> the misleading impression that obtaining the  *n*th key/value from a
> dictionary is just as efficient as obtaining the *n*th item from a list
> or tuple.
>
> Best,
> Alex
>
> On 10 Oct 2021, at 05:05, Finn Mason <finnjavie...@gmail.com> wrote:
>
> 
>
> On Sat, Oct 9, 2021, 9:56 PM Steven D'Aprano <st...@pearwood.info> wrote:
>
>> [Snip...]
>
>
> Newbies won't know first() lives in itertools, and those experienced
>> enough to know it is there probably won't bother to use it.
>>
>
> A very good point.
>
> Let's get back to the original topic. Should `dict.items()` be indexable
> now that dicts are ordered? I say yes. Why shouldn't it?
>
>
> --
> Finn Mason
>
>> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/OOR2AUMA7UMHHVW7XLLUXHTNKGRXTPU4/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/RHNNAZR2ZBYZFQ75VHR3FUMVY6GWDDB6/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-- 
--Guido (mobile)
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BISXD643VSNQVE6RFPWGZNH5SL3GE3QE/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to