here is the thread from the last time that this was brought up:
https://mail.python.org/archives/list/python-ideas@python.org/thread/S7UMTWK65X6BJDYZ3SSU7I7HOIASDMMJ/#S7UMTWK65X6BJDYZ3SSU7I7HOIASDMMJ

Thanks, that's very helpful. Sounds like Guido is right, and the short answer is "while there would be ways of making it fast (and faster than the status quo) for small dicts, there is probably no way of making it O(1), as indexing is for lists and tuples".

For me, that's a deal-breaker. I think if you're adding in a way to get the nth key of a dict by sequence-style indexing, it's going to be very surprising for newbies if they find out that getting the 8743rd key of the dict takes a lot longer than getting the 1st key of the dict, given that that's not at all the behaviour of lists and tuples.

Best,
Alex

On Sun, Oct 10, 2021 at 7:26 PM Christopher Barker <pythonchb@gmail.com> wrote:
here is the thread from the last time that this was brought up:

https://mail.python.org/archives/list/python-ideas@python.org/thread/S7UMTWK65X6BJDYZ3SSU7I7HOIASDMMJ/#S7UMTWK65X6BJDYZ3SSU7I7HOIASDMMJ

It was very thoroughly discussed then.

-CHB


On Sun, Oct 10, 2021 at 8:33 AM Guido van Rossum <guido@python.org> wrote:
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.waygood@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 nth 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  nth key/value from a dictionary is just as efficient as obtaining the nth item from a list or tuple.

Best,
Alex

On 10 Oct 2021, at 05:05, Finn Mason <finnjavier08@gmail.com> wrote:


On Sat, Oct 9, 2021, 9:56 PM Steven D'Aprano <steve@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-leave@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-leave@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-leave@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/


--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython