
On Mon, Jun 29, 2020 at 11:57 AM Stestagg <stestagg@gmail.com> wrote:
I'm quite supportive (+1) of the proposal to add numeric indexing to the 'dict_*' views.
Given that dictionaries are now ordered, it seems reasonable to look-up, for example, keys by index,
Several times now, I've had the need to 'just get any key/value' from a large dictionary. I usually try first to run `var.keys()[0]` only to be told that I'm not allowed to do this, and instead have to ask python to make a copy of this datastructure with a different type, just so I can
My first thought was no -- even though dicts are ordered, they really aren't a Sequence in the same way. But making the various dict sequences does add some convenience, and I can't see how it would cause any problems. As it happens, I have run into the use cases @Stestagg presents. But I'm curious -- what do people see as the downsides? I'm not coming up with any. Note: there may be implementation issues -- you couldn't really do it efficiently without poking in to the inner workings of the dict object, but I imagine that's surmountable. perform the index operation. This is possible, but seems redundant, and reinforces bad practices around creating copies of potentially large structures. the other solution to that is to call iter() and next(): In [2]: a_dict Out[2]: {0: 5, 1: 6, 2: 7, 3: 8, 4: 9} In [3]: next(iter(a_dict.items())) Out[3]: (0, 5) But yeah, that's pretty klunky -- and gets quite inefficient if you want something other than the first one. -CHB
Another use-cases is doing variations of reduce() over dictionaries, where getting an initial value from the dict, and then performing operations over the remaining items is much simpler to do with indexing on the views.
Steve
On Mon, Jun 29, 2020 at 1:27 PM Hans Ginzel <hans@matfyz.cz> wrote:
Thank you.
On Fri, Jun 26, 2020 at 02:50:22PM -0300, Joao S. O. Bueno wrote:
On Fri, 26 Jun 2020 at 14:30, Hans Ginzel <hans@matfyz.cz> wrote:
thank you for making dict ordered. Is it planned to access key,value pair(s) by index? See https://stackoverflow.com/a/44687752/2556118 for example. Both for reading and (re)writing? Is it planned to insert pair(s) on exact index? Or generally to slice? See splice() in Perl, https://perldoc.perl.org/functions/splice.html. …
These are odd requirements.
No - Python dictionaries are ordered, by order of insertion only, but one can't generally do any manipulation by the numeric index of a dictionary entry - and it will stay that way.
That is fully corret to respect the _insertion_ order.
If you need such an hybrid data structure, you could just have a list of tuples as data structure, and use
collections.abc.MutableMapping >to provide a dict-like interface to it (an index for better than linear search). > >I could create such a data structure if you want,
Thank you, I will write it myself. H. _______________________________________________ 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/BWHRHY... 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/32DNWT... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython