On Thu, Jul 30, 2020 at 08:52:34AM -0700, Christopher Barker wrote:

> The point being that dicts are now ordered, so maybe it makes sense to add
> some operations that make it natural and easy to work with them in an
> ordered way -- you know, like slicing :-)

Dicts are not sequences and cannot support ordering operations like 
insert, sort, prepend, reverse or slice assignment. They merely preserve 
insertion order.

You can't insert a key in a specific position. If I have this dict:

    mydict = {'a': 1, 'c': 3, 'd': 4, 'e': 5}

I can't insert 'b':2 between keys 'a' and 'c', except by creating a new 
dict. Nor can I sort the keys, or reverse them, except by putting them 
into a list and sorting or reversing the list, and only then putting 
them into a dict.

We could read the key and/or value at index N, that is straight forward 
if we can decide on a colour of the bike shed. But I don't believe that 
there is an straight forward way of replacing the key at index N, or of 
inserting keys, or swapping them, or directly changing their order.

Dicts are, sort of, like a stack: you can "insert" (append) new keys at 
the end, but not in the middle.

So let's please stop over-generalising by saying dicts are "ordered" 
without the qualifier that they only preserve insertion order, just like 
collections.OrderedDict.

The minimal API we could support would be a getter that returns the key 
at index N. Once you have the key, you can get or set the value. So all 
we really need is that one single getter. Anything else is gravy.

    dict.getkey(n)  # returns the key at index n by insertion order

I wouldn't object to:

    dict.getitem(n)  # returns (key, value) at index n

since it's hardly more trouble to return both the key and the value at 
the same time. But more than that seems difficult to justify, and 
supporting slices seems like overkill. You can always get them with a 
comprehension:

    [mydict.getkey(n) for n in range(3, 49, 7)]


-- 
Steven
_______________________________________________
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/ICROBIPAJEXZUV3J7U2QTTCGUL7HIODU/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to