[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-09-14 Thread Marco Sulla
I just had an idea: we have mp->ma_used and keys->dk_nentries. holes = mp->ma_keys->dk_nentries - mp->ma_used is the number of holes in the dict. So, if holes == 0, you have O(1). But, if the dict has holes, you can't have O(1), but you can speedup the check of the entry by counting the NULLs in

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-09-01 Thread Inada Naoki
On Mon, Aug 31, 2020 at 9:30 PM wrote: > I have a use case which relates to this request: iterating over a dict > starting from a given key. I would like to achieve this without having to pay > the full O(n) cost if I'm going to be iterating over only a few items. My > understanding is that

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-09-01 Thread Dominik Vilsmeier
On 01.09.20 11:22, Zig Zigson wrote: I believe I described my case poorly, the process to get from one state (key) to the next is an external (slow) process; the value stored is not the next state but a value calculated while advancing the state. This dict serves as a way to quickly skip

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-09-01 Thread Zig Zigson
I believe I described my case poorly, the process to get from one state (key) to the next is an external (slow) process; the value stored is not the next state but a value calculated while advancing the state. This dict serves as a way to quickly skip steps of the external process when it has

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-09-01 Thread Zig Zigson
Having gotten to an implementation, you are correct, the dict iteration does not take the lion's share, or at least there are several other steps in my application which dwarf the dict traversal operations in any case. I don't think I in practice have a compelling case here, so all I'm left with

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-09-01 Thread Dominik Vilsmeier
On 31.08.20 06:01, junkneno...@gmail.com wrote: I have a use case which relates to this request: iterating over a dict starting from a given key. I would like to achieve this without having to pay the full O(n) cost if I'm going to be iterating over only a few items. My understanding is that

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-31 Thread Steven D'Aprano
On Mon, Aug 31, 2020 at 04:01:06AM -, junkneno...@gmail.com wrote: > I have a use case which relates to this request: iterating over a dict > starting from a given key. I would like to achieve this without having > to pay the full O(n) cost if I'm going to be iterating over only a few >

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-31 Thread junknenopok
I have a use case which relates to this request: iterating over a dict starting from a given key. I would like to achieve this without having to pay the full O(n) cost if I'm going to be iterating over only a few items. My understanding is that this should be achievable without needing to

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-03 Thread Inada Naoki
On Tue, Aug 4, 2020 at 3:35 AM Christopher Barker wrote: > > On Sat, Aug 1, 2020 at 6:10 PM Inada Naoki wrote: >> >> Repacking is mutation, and mutating dict while iterating it breaks the >> iterator. >> But `d.items()[42]` don't looks like mutation. > > > Pardon my ignorance, but IS repacking

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-03 Thread Christopher Barker
On Sat, Aug 1, 2020 at 6:10 PM Inada Naoki wrote: > Repacking is mutation, and mutating dict while iterating it breaks the > iterator. > But `d.items()[42]` don't looks like mutation. > Pardon my ignorance, but IS repacking a mutation? Clearly it's mutating the internal state, but at the

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-02 Thread Wes Turner
https://github.com/python/cpython/blob/master/Objects/odictobject.c : """ In order to preserve O(1) performance for node removal (finding nodes), we must do better than just looping through the linked-list. Here are options we've considered: 1. use a second dict to map keys to nodes (a la the

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-02 Thread Marco Sulla
On Sat, 1 Aug 2020 at 20:25, Stestagg wrote: > I wrote some (better than the previously shared) benchmarks for this > change a while ago. > I think that you could speed up the algorithm if you check if dict->ma_keys->dk_lookup == lookdict_unicode_nodummy. If so, the dict is a combined dict with

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-02 Thread Marco Sulla
On Sat, 1 Aug 2020 at 19:34, Christopher Barker wrote: > I would think the goal here would be to re-order once in a while to remove > the holes. But that would take time, of course, so you wouldn't want to do > it on every deletion. But when? > You should have a separate process that does this,

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Steven D'Aprano
On Sat, Aug 01, 2020 at 07:25:42PM +0100, Stestagg wrote: > Irrespective of where in the api this logic should exist, the > implementation won't be algorithmically different, (I think, even with a > `.ordered` view, as the view would have to cope with changes to the > underlying dictionary over

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Wes Turner
Is there any reason that these features couldn't be added to OrderedDict (which is a linked list)? https://github.com/python/cpython/blob/master/Objects/odictobject.c On Sat, Aug 1, 2020, 9:13 PM Inada Naoki wrote: > On Sun, Aug 2, 2020 at 2:34 AM Christopher Barker > wrote: > > > > On Sat,

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Inada Naoki
On Sun, Aug 2, 2020 at 2:34 AM Christopher Barker wrote: > > On Sat, Aug 1, 2020 at 2:28 AM Marco Sulla > wrote: >> >> On Sat, 1 Aug 2020 at 03:00, Inada Naoki wrote: >>> >>> Please teach me if you know any algorithm which has no hole, O(1) >>> deletion, preserving insertion order, and

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Wes Turner
first() https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.first https://toolz.readthedocs.io/en/latest/api.html#toolz.itertoolz.first last() https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.last

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Guido van Rossum
Yeah, it is totally doable to refactor the collection ABCs to have something in between `Collection` and `Sequence` that just supports `__getitem__`. But I would take Marco's research (and Inada's musings) seriously -- we don't actually want to support `__getitem__`, because of the unpredictable

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Christopher Barker
On Fri, Jul 31, 2020 at 7:34 AM Guido van Rossum wrote: > So maybe we need to add dict.ordered() which returns a view on the items > that is a Sequence rather than a set? Or ordereditems(), orderedkeys() and > orderedvalues()? > I'm still confused as to when "ordered" became synonymous with

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Stestagg
I wrote some (better than the previously shared) benchmarks for this change a while ago. They are run on cPython with a patch applied that implements dict_views __getitem__() using a method similar to `lookdict` to perform indexing on keys/values/etc. Irrespective of where in the api this logic

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Tim Peters
[Steven D'Aprano ] >> >> The other simple solution is `next(iter(mydict.items()))`. [Guido] > That one always makes me uncomfortable, because the StopIteration it > raises when the dict is empty might be misinterpreted. Basically I never > want to call next() unless there's a try...except

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Christopher Barker
On Sat, Aug 1, 2020 at 2:28 AM Marco Sulla wrote: > On Sat, 1 Aug 2020 at 03:00, Inada Naoki wrote: > >> Please teach me if you know any algorithm which has no hole, O(1) >> deletion, preserving insertion order, and efficient and fast as array. >> > I would think the goal here would be to

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Marco Sulla
On Sat, 1 Aug 2020 at 03:00, Inada Naoki wrote: > Please teach me if you know any algorithm which has no hole, O(1) > deletion, preserving insertion order, and efficient and fast as array. > :) About the hole, I was thinking that in theory the problem can be circumvented using a modified

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Steven D'Aprano
On Sat, Aug 01, 2020 at 03:57:34PM +1000, Chris Angelico wrote: > Ahh, okay. So it's safe in the sense that it can't accidentally leak a > confusing exception. Unfortunately a lot of people are going to assume > that it means "will always give a useful return value". Definitely > worth being very

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Christopher Barker
On Sat, Aug 1, 2020 at 10:19 AM Wes Turner wrote: > > AFAIU, direct subscripting / addressing was not a use case in the design > phase of the current dict? > Nope, -- if it were, it would have presumably been implemented :-) But order-preserving wasn't really a design goal either, as I

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Inada Naoki
On Sat, Aug 1, 2020 at 12:40 PM Steven D'Aprano wrote: > > On Fri, Jul 31, 2020 at 08:08:58PM -0700, Guido van Rossum wrote: > > > > The other simple solution is `next(iter(mydict.items()))`. > > > > > > > That one always makes me uncomfortable, because the StopIteration it raises > > when the

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-08-01 Thread Chris Angelico
On Sat, Aug 1, 2020 at 3:50 PM Steven D'Aprano wrote: > > On Sat, Aug 01, 2020 at 02:08:08PM +1000, Chris Angelico wrote: > > On Sat, Aug 1, 2020 at 1:43 PM Steven D'Aprano wrote: > > > > Some years ago, someone (I think it was Nick Coghlan?) proposed a > > > standard solution for this issue, a

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Steven D'Aprano
On Sat, Aug 01, 2020 at 02:08:08PM +1000, Chris Angelico wrote: > On Sat, Aug 1, 2020 at 1:43 PM Steven D'Aprano wrote: > > Some years ago, someone (I think it was Nick Coghlan?) proposed a > > standard solution for this issue, a context manager + decorator function > > that guarded against a

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Chris Angelico
On Sat, Aug 1, 2020 at 1:43 PM Steven D'Aprano wrote: > Some years ago, someone (I think it was Nick Coghlan?) proposed a > standard solution for this issue, a context manager + decorator function > that guarded against a specific exception. Nothing much came of it, but > I did experiment with

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Wes Turner
On Fri, Jul 31, 2020 at 10:59 PM Steven D'Aprano wrote: > [...] The first request in this thread was from Hans: > > > https://mail.python.org/archives/list/python-ideas@python.org/message/S7UMTWK65X6BJDYZ3SSU7I7HOIASDMMJ/ > > He is using a dict to hold an array of columns indexed by name >

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Steven D'Aprano
On Fri, Jul 31, 2020 at 08:08:58PM -0700, Guido van Rossum wrote: > > The other simple solution is `next(iter(mydict.items()))`. > > > > That one always makes me uncomfortable, because the StopIteration it raises > when the dict is empty might be misinterpreted. Basically I never want to > call

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Steven D'Aprano
On Fri, Jul 31, 2020 at 04:30:22PM -0700, Guido van Rossum wrote: > > So: > > dicts preserve order > > the dict_views preserve this same order > > > > Some think that we can't call them "ordered", because somehow that implies > > other things, like the ability to insert, etc, but while I don't

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Guido van Rossum
On Fri, Jul 31, 2020 at 7:59 PM Steven D'Aprano wrote: > On Fri, Jul 31, 2020 at 04:08:43PM -0700, Guido van Rossum wrote: > [...] > > I'm guessing that indexing by 0, if it were possible, would be a > convenient > > idiom to implement the "first item" operation that has been requested > >

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Steven D'Aprano
On Fri, Jul 31, 2020 at 04:08:43PM -0700, Guido van Rossum wrote: > Maybe it is common in numpy and pandas to keep adding operations to the > same object until it breaks, but the key and items views already implement > the Set ABC, and I'd rather refrain from having them *also* implement the >

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Inada Naoki
On Sat, Aug 1, 2020 at 10:19 AM Wes Turner wrote: > > We should be reading the source: > https://github.com/python/cpython/blob/master/Objects/dictobject.c > > AFAIU, direct subscripting / addressing was not a use case in the design > phase of the current dict? > > Could a

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Inada Naoki
On Sat, Aug 1, 2020 at 8:14 AM Christopher Barker wrote: > > > If that's all we've come up with in this lengthy thread, it's probably not > worth it -- after all, both of those can be efficiently accomplished with a > little help from itertools.islice and/or next(). > 100% agree. > But I

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Wes Turner
We should be reading the source: https://github.com/python/cpython/blob/master/Objects/dictobject.c AFAIU, direct subscripting / addressing was not a use case in the design phase of the current dict? Could a __getitem__(slice_or_int_index) be implemented which just skips over the NULLs? Or would

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Inada Naoki
On Sat, Aug 1, 2020 at 10:04 AM Wes Turner wrote: > > Actually, I think the reverse traversal case is the worst case because: it's > not possible to use negative subscripts with islice (because that would > require making a full copy). > > This doesn't work: > >>> islice(dict.keys(), -1, -5) >

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Marco Sulla
Ok... I wrong. The array of items have a NULL key and value if one item is deleted. Sorry for the confusion. On Sat, 1 Aug 2020 at 01:09, Guido van Rossum wrote: > the key and items views already implement the Set ABC, and I'd rather > refrain from having them *also* implement the Sequence ABC.

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Inada Naoki
On Sat, Aug 1, 2020 at 9:55 AM Marco Sulla wrote: > > On Sat, 1 Aug 2020 at 02:30, Stestagg wrote: >> >> The dict keys is compact only *until* you delete an item, at which point, a >> hole is left in the array > > No, the array of items has no hole. The hole is inserted in the hashtable. Yes,

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Wes Turner
Actually, I think the reverse traversal case is the worst case because: it's not possible to use negative subscripts with islice (because that would require making a full copy). This doesn't work: >>> islice(dict.keys(), -1, -5) Reverse traversal did work in Python 2 but was foregone when making

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Wes Turner
On Fri, Jul 31, 2020 at 8:44 PM Guido van Rossum wrote: > This is not great news. A solution could be to make dict.ordered() force > compaction -- if there are no deleted keys this would be a no-op. We'd have > to be able to tell in constant time whether this is the case. But yeah, > this would

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Marco Sulla
On Sat, 1 Aug 2020 at 02:30, Stestagg wrote: > The dict keys is compact only *until* you delete an item, at which point, > a hole is left in the array > No, the array of items has no hole. The hole is inserted in the hashtable. ___ Python-ideas

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Guido van Rossum
This is not great news. A solution could be to make dict.ordered() force compaction -- if there are no deleted keys this would be a no-op. We'd have to be able to tell in constant time whether this is the case. But yeah, this would be a dangerous thing in the hands of folks below wizard level.

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Stestagg
On Sat, 1 Aug 2020 at 00:32, Guido van Rossum wrote: > If true this would be a huge argument against adding indexing and slicing > (other than the special case starting with 0). However, I don't think it's > true. The dict implementation (again, starting in 3.6) actually stores the > list of

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Guido van Rossum
On Fri, Jul 31, 2020 at 4:12 PM Christopher Barker wrote: > On Fri, Jul 31, 2020 at 2:56 PM Marco Sulla > wrote: > >> Yes. Since now dicts are ordered by insertion, also keys, values and >> items are ordered the same way. >> > > Preservation of oider was codified in version 3.7 (? at some

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Christopher Barker
On Fri, Jul 31, 2020 at 2:56 PM Marco Sulla wrote: > Yes. Since now dicts are ordered by insertion, also keys, values and items > are ordered the same way. > Preservation of oider was codified in version 3.7 (? at some point, anyway). And while it may not explicitly say that the views will

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Guido van Rossum
On Fri, Jul 31, 2020 at 2:56 PM Marco Sulla wrote: > On Fri, 31 Jul 2020 at 22:14, Chris Angelico wrote: > >> So, constructing a tuple or list from the keys or items WILL give you a >> sequence. >> > > Yes. Since now dicts are ordered by insertion, also keys, values and items > are ordered the

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Marco Sulla
On Fri, 31 Jul 2020 at 22:14, Chris Angelico wrote: > So, constructing a tuple or list from the keys or items WILL give you a > sequence. > Yes. Since now dicts are ordered by insertion, also keys, values and items are ordered the same way. It seems to me more simple to add some sequence

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Chris Angelico
On Sat, Aug 1, 2020 at 6:07 AM Ricky Teachey wrote: > > On Fri, Jul 31, 2020 at 3:52 PM Marco Sulla > wrote: >> >> Is it not more simple to add some sequence methods to the dict views (if >> there's a real need)? >> If you do tuple(dict.keys()), you get the sequence of keys in the same >>

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Ricky Teachey
On Fri, Jul 31, 2020 at 3:52 PM Marco Sulla wrote: > Is it not more simple to add some sequence methods to the dict views (if > there's a real need)? > If you do tuple(dict.keys()), you get the sequence of keys in the same > insertion order of the dict. It seems to me that the duck already

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Marco Sulla
Is it not more simple to add some sequence methods to the dict views (if there's a real need)? If you do tuple(dict.keys()), you get the sequence of keys in the same insertion order of the dict. It seems to me that the duck already exists and quacks. ___

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Wes Turner
> keyseq(), valueseq(), itemseq() Yeah, that's a good one On Fri, Jul 31, 2020, 12:18 PM Ricky Teachey wrote: > On Fri, Jul 31, 2020 at 11:55 AM Wes Turner wrote: > >> +1 on (lazy) Sequence views optimized for sequential reads and random >> access (which hide the dict implementation which

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Ricky Teachey
On Fri, Jul 31, 2020 at 11:55 AM Wes Turner wrote: > +1 on (lazy) Sequence views optimized for sequential reads and random > access (which hide the dict implementation which varies across which > platforms?) > > I'm somewhat indifferent on the name: > > # Sequence views names: > ## method() ->

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Wes Turner
+1 on (lazy) Sequence views optimized for sequential reads and random access (which hide the dict implementation which varies across which platforms?) I'm somewhat indifferent on the name: # Sequence views names: ## method() -> Sequence - orderedkeys(), orderedvalues(), ordereditems() - okeys(),

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Ricky Teachey
On Fri, Jul 31, 2020 at 10:31 AM Guido van Rossum wrote: > So maybe we need to add dict.ordered() which returns a view on the items > that is a Sequence rather than a set? Or ordereditems(), orderedkeys() and > orderedvalues()? > > I for one would be +1 on this idea. On the bike shed hew:

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Guido van Rossum
So maybe we need to add dict.ordered() which returns a view on the items that is a Sequence rather than a set? Or ordereditems(), orderedkeys() and orderedvalues()? On Fri, Jul 31, 2020 at 05:29 Ricky Teachey wrote: > On Fri, Jul 31, 2020, 2:48 AM Wes Turner wrote: > >> # Dicts and DataFrames

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Ricky Teachey
On Fri, Jul 31, 2020, 2:48 AM Wes Turner wrote: > # Dicts and DataFrames > - Src: > https://github.com/westurner/pythondictsanddataframes/blob/master/dicts_and_dataframes.ipynb > - Binder: > https://mybinder.org/v2/gh/westurner/pythondictsanddataframes/master?filepath=dicts_and_dataframes.ipynb

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-31 Thread Wes Turner
# Dicts and DataFrames - Src: https://github.com/westurner/pythondictsanddataframes/blob/master/dicts_and_dataframes.ipynb - Binder: https://mybinder.org/v2/gh/westurner/pythondictsanddataframes/master?filepath=dicts_and_dataframes.ipynb - (interactive Jupyter Notebook hosted by

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Marco Sulla
On Thu, 30 Jul 2020 at 23:42, Steven D'Aprano wrote: > Of course anything is possible in theory, but I think that would require > shifting the existing keys and values in the array, and updating the > entries in the hash table to point to their key in the new position, and > probably other

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Guido van Rossum
On Thu, Jul 30, 2020 at 22:23 Greg Ewing wrote: > On 31/07/20 4:04 am, Christopher Barker wrote: > > (Side note: why ARE the views provided by methods, rather than > > properties?) > > Probably because they construct objects, and are therefore > somewhat more expensive than is usually expected

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Greg Ewing
On 31/07/20 4:04 am, Christopher Barker wrote: (Side note: why ARE the views provided by methods, rather than properties? Probably because they construct objects, and are therefore somewhat more expensive than is usually expected for an attribute access. -- Greg

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Steven D'Aprano
On Thu, Jul 30, 2020 at 06:17:24PM -0400, Wes Turner wrote: > What new syntax do you propose? I don't propose new syntax. If this functionality is needed, regular method syntax will do the job without misleading people to th8ink that dicts are sequences that ought to support the full sequence

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Wes Turner
What new syntax do you propose? On Thu, Jul 30, 2020 at 5:55 PM Steven D'Aprano wrote: > On Thu, Jul 30, 2020 at 05:03:58PM -0400, Wes Turner wrote: > > > Would these be the *non-mutating* methods desired of insertion-ordered > > dicts? > > > > .iloc[sli:ce] > > .iloc[int] > > .iloc[[list,]] >

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Steven D'Aprano
On Thu, Jul 30, 2020 at 05:03:58PM -0400, Wes Turner wrote: > Would these be the *non-mutating* methods desired of insertion-ordered > dicts? > > .iloc[sli:ce] > .iloc[int] > .iloc[[list,]] > .iloc[callable] > .iloc[bitmask] > > .index(key) No. "iloc" sounds like a new Apple product to keep

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Steven D'Aprano
On Thu, Jul 30, 2020 at 09:35:31PM +0200, Marco Sulla wrote: > On Thu, 30 Jul 2020 at 19:24, Steven D'Aprano wrote: > > > 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

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Wes Turner
Are you suggesting that dict views would become subscriptable? In [3]: %doctest_mode Exception reporting mode: Plain Doctest mode is: ON >>> x = dict(a=2, b=3, c=4) >>> x.items()[3] Traceback (most recent call last): File "", line 1, in x.items()[3] TypeError: 'dict_items' object is not

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Wes Turner
On Thu, Jul 30, 2020, 3:37 PM Marco Sulla wrote: > On Thu, 30 Jul 2020 at 19:24, Steven D'Aprano wrote: > >> 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

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Marco Sulla
On Thu, 30 Jul 2020 at 19:24, Steven D'Aprano wrote: > 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. > Not sure about this. In C code,

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Steven D'Aprano
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

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread David Mertz
On Thu, Jul 30, 2020 at 11:55 AM Christopher Barker wrote: > > smaller_dict = dict(islice(large_dict.items(), 0, 255)) > well, beauty is in the eye of the beholder, of course. But really, you > think it's pretty to import itertools, then make a function call, for > what's very much a slicing

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Christopher Barker
On Wed, Jul 29, 2020 at 7:23 AM Wes Turner wrote: > .iloc[] is the Pandas function for accessing by integer-location: > > https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html > I'm not sure I quite get the analogy here, but ... >>> odict.iloc[3] > > > Would

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-30 Thread Christopher Barker
On Tue, Jul 28, 2020 at 10:26 PM Stephen J. Turnbull < turnbull.stephen...@u.tsukuba.ac.jp> wrote: > Christopher Barker writes: > > from itertools import islice > > > > smaller_dict = dict(islice(large_dict.items(), 0, 255)) > > > > which works, and isn't doing any unnecessary copying, but

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-29 Thread Wes Turner
.iloc[] is the Pandas function for accessing by integer-location: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html """ Purely integer-location based indexing for selection by position. .iloc[] is primarily integer position based (from 0 to length-1 of the

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-28 Thread Stephen J. Turnbull
Christopher Barker writes: > from itertools import islice > > smaller_dict = dict(islice(large_dict.items(), 0, 255)) > > which works, and isn't doing an unnecessary copying but it's pretty > darn ugly, as far as I'm concerned. In your application, I think that's just pretty, myself.

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-28 Thread Christopher Barker
re-awakening this thread, because I just happened on an actual real-world use case: I need the first 255 items in a dict, as a dict. Order is important, and I can count on an order-preserving dict. I ended up with: from itertools import islice smaller_dict = dict(islice(large_dict.items(), 0,

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-21 Thread Wes Turner
On Mon, Jul 13, 2020, 3:04 PM Christopher Barker wrote: > Is there ANY chance of setting the default reply-to to the list? I know > everyone else thinks that's a bid idea, but I doubt this was actually > intended just for me. > > If it was, I apologize for bringing it back on the list. > > On

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-21 Thread Guido van Rossum
I think this cannot just be considered a bug fix, and it seems somewhat fundamental (catching arbitrary exceptions is controversial), so I recommend finding a core dev to sponsor a PEP. (Or finding one who thinks it is obviously a bug and will approve a PR.) On Tue, Jul 21, 2020 at 04:43 Dominik

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-18 Thread Rob Cliffe via Python-ideas
On 13/07/2020 18:42, Barry Scott wrote: On 13 Jul 2020, at 05:55, Christopher Barker > wrote: well, sure, though I have to say that I think that that's an unfortunate confusing thing about python dicts. IN fact, I doubt there are many uses at all for

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-14 Thread Barry Scott
> On 13 Jul 2020, at 18:56, Paul Moore wrote: > > On Mon, 13 Jul 2020 at 18:42, Barry Scott wrote: > >> On 13 Jul 2020, at 05:55, Christopher Barker wrote: >> >> well, sure, though I have to say that I think that that's an unfortunate >> confusing thing about python dicts. IN fact, I

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-13 Thread Christopher Barker
Is there ANY chance of setting the default reply-to to the list? I know everyone else thinks that's a bid idea, but I doubt this was actually intended just for me. If it was, I apologize for bringing it back on the list. On Sun, Jul 12, 2020 at 10:12 PM Inada Naoki wrote: > On Mon, Jul 13,

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-13 Thread Christopher Barker
On Mon, Jul 13, 2020 at 11:15 AM Rob Cliffe wrote: > On 13 Jul 2020, at 05:55, Christopher Barker wrote: > > In fact, I doubt there are many uses at all for dict.keys() -- most uses > can jsut use the dict. > > But you could use items() instead: > for name, val in sorted(dict.items()): >

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-13 Thread Christopher Barker
>> I doubt there are many uses at all for dict.keys() -- most uses can jsut use the dict. > > > > I use key() all the time to sort the keys before printing. > > > > for name in sorted(dict.key()): > > print(name, dict[name) > > Why not just use sorted(dict)? Thanks for so nicely making my

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-13 Thread Paul Moore
On Mon, 13 Jul 2020 at 18:42, Barry Scott wrote: > On 13 Jul 2020, at 05:55, Christopher Barker wrote: > > well, sure, though I have to say that I think that that's an unfortunate > confusing thing about python dicts. IN fact, I doubt there are many uses at > all for dict.keys() -- most uses

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-13 Thread Barry Scott
> On 13 Jul 2020, at 05:55, Christopher Barker wrote: > > well, sure, though I have to say that I think that that's an unfortunate > confusing thing about python dicts. IN fact, I doubt there are many uses at > all for dict.keys() -- most uses can jsut use the dict. I use key() all the time

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-12 Thread Christopher Barker
On Sat, Jul 11, 2020 at 1:33 PM David Mertz wrote: > On Sat, Jul 11, 2020 at 3:45 PM Christopher Barker > wrote: > >> random.choice(the_dict.keys()) >> >> is a little easier than: >> >> random.choice(list(the_dict.keys()) >> > > Ummm... don't you mean: > > random.choice(list(the_dict)) > well,

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-11 Thread Inada Naoki
On Sun, Jul 12, 2020 at 4:43 AM Christopher Barker wrote: > > > The existing dictionary memory layout doesn't support direct indexing > > (without stepping), so this functionality is not being added as a > > requirement. > > But it does make it much more efficient if the stepping is done inside

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-11 Thread David Mertz
On Sat, Jul 11, 2020 at 4:33 PM David Mertz wrote: > In any case, if "reservoir sampling" is the goal here, we should just add > a function `random.reservoir_sample()` to accommodate using iterators > rather than sequences (https://en.wikipedia.org/wiki/Reservoir_sampling) > Doh! I mean

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-11 Thread David Mertz
On Sat, Jul 11, 2020 at 3:45 PM Christopher Barker wrote: > random.choice(the_dict.keys()) > > is a little easier than: > > random.choice(list(the_dict.keys()) > Ummm... don't you mean: random.choice(list(the_dict)) If it's keys you care about I've saved you one character over your proposed

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-11 Thread David Mertz
On Sat, Jul 11, 2020 at 3:45 PM Christopher Barker wrote: > On Fri, Jul 10, 2020 at 12:45 PM David Mertz wrote: > > The strongest argument I've seen is: `list(d.items())` adds six > characters. > > 1) Matching our mental model / usability: if I want the nth item (or a > random item) from a

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-11 Thread Christopher Barker
I had a nice note almost written yesterday, but now there've been a bunch more discussion, so I'm going to try to hit a few points that have been recently made. TL;DR: I personally think it would be a nice feature to add indexing to the dict views. But to be fair, the only real use case I've seen

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-11 Thread Random832
On Thu, Jul 9, 2020, at 13:26, Stestagg wrote: > Obviously, python is a general-purpose, turing complete language, so > each of these options can be written in other ways. But it would be > nice if the simple, readable versions also worked :D > > The idea that there are future, unspecified

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-11 Thread Stephen J. Turnbull
Chris Angelico writes: > I would pick repeatedly from the same dictionary but it might be > mutated in between. So the list would have to be reconstructed > fresh every time. OK, that moves me a couple million Planck lengths away from -1 nm. :-) I guess in that case if I cared about

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Steven D'Aprano
On Fri, Jul 10, 2020 at 06:06:03PM +0100, Stestagg wrote: > I don't believe that this feature would steepen the language learning curve > however, but actually help to shallow it slightly (Explained more below) It steepens it because it's yet another feature to learn. Is it dicts or dict views

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Inada Naoki
I'm -1 too. Making ordered containers to sequence-like only for random.choice is a wrong idea. If random.choice should support non-sequence ordered container, just propose it to random.choice. And if the proposal was rejected, you can do it by yourself with helper functions. >>> import random

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread David Mertz
Definitely -1 after reading all the discussion. The strongest argument I've seen is: `list(d.items())` adds six characters. So far, there's absolutely nothing described that cannot easily be accomplished with list(). Moreover, even apart from the work of maintaining the feature itself, the

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Stestagg
> I too have sometimes proposed what I think of as "minor quality-of-life" > enhancements, and had them shot down. It stings a bit, and can be > frustrating, but remember it's not personal. > I don't mind the shooting down, as long as the arguments make sense :D. It seems like we're both in

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Chris Angelico
On Sat, Jul 11, 2020 at 12:45 AM Steven D'Aprano wrote: > > On Fri, Jul 10, 2020 at 02:50:17PM +1000, Chris Angelico wrote: > > > And immediately above that part, I said that I had made use of this, > > and had used it in Python by listifying the dict first. Okay, so I > > didn't actually dig up

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Ronald Oussoren via Python-ideas
> On 10 Jul 2020, at 15:38, Eric V. Smith wrote: > > > On 7/10/2020 9:20 AM, Paul Moore wrote: >> On Fri, 10 Jul 2020 at 13:47, Stestagg wrote: >> >>> The intent of my statement was: The current implementation of dict does not >>> allow any reasonable implementation of

[Python-ideas] Re: Access (ordered) dict by index; insert slice

2020-07-10 Thread Steven D'Aprano
On Fri, Jul 10, 2020 at 02:50:17PM +1000, Chris Angelico wrote: > And immediately above that part, I said that I had made use of this, > and had used it in Python by listifying the dict first. Okay, so I > didn't actually dig up the code where I'd done this, but that's a use > case. I have

  1   2   >