dict_items.__getitem__?
Have folks thought about allowing indexing dictionary views as in the following code, where d is a dict object? d.keys()[0] d.keys()[-1] d.values()[0] d.values()[-1] d.items()[0] d.items()[-1] # item that would be returned by d.popitem() I could see value to the last form in particular: you might want to inspect the last item of a dictionary before possibly popping it. I've also often wanted to get an arbitrary item/key from a dictionary, and d.items()[0] seems natural for this. Of course, the universal way to get the first item from an iterable x is item = next(iter(x)) I can't say this is particularly readable, but it is functional and fast. Or sometimes I use this pattern: for item in x: break If you wanted the last item of a dictionary d (the one to be returned from d.popitem()), you could write this beautiful code: last = next(iter(reversed(d.items()))) Given the dictionary order guarantee from Python 3.7, adding indexing (__getitem__) to dict views seems natural. The potential catch is that (I think) it would require linear time to access an item in the middle, because you need to count the dummy elements. But accessing [i] and [-i] should be doable in O(|i|) time. (I've wondered about the possibility of doing binary or interpolation search, but without some stored index signposts, I don't think it's possible.) Python is also full of operations that take linear time to do: list.insert(0, x), list.pop(0), list.index(), etc. But it may be that __getitem__ takes constant time on all built-in data structures, and the apparent symmetry but very different performance between dict()[i] and list()[i] might be confusing. That said, I really just want d[0] and d[-1], which is when these are fast. I found some related discussion in https://mail.python.org/archives/list/python-ideas@python.org/thread/QVTGZD6... but not this exact idea. Erik -- Erik Demaine | edemaine@mit.edu | http://erikdemaine.org/
On Tue, Oct 5, 2021 at 9:46 AM Erik Demaine <edemaine@mit.edu> wrote:
Of course, the universal way to get the first item from an iterable x is
item = next(iter(x))
I can't say this is particularly readable, but it is functional and fast.
I think we can add `itertools.first()` for this idiom, and `itertools.last()` for `next(iter(reversed(x)))` idiom.
Given the dictionary order guarantee from Python 3.7, adding indexing (__getitem__) to dict views seems natural. The potential catch is that (I think) it would require linear time to access an item in the middle, because you need to count the dummy elements. But accessing [i] and [-i] should be doable in O(|i|) time.
It is not true. They are O(n) where n is the maximum dict size.
Python is also full of operations that take linear time to do: list.insert(0, x), list.pop(0), list.index(), etc. But it may be that __getitem__ takes constant time on all built-in data structures, and the apparent symmetry but very different performance between dict()[i] and list()[i] might be confusing. That said, I really just want d[0] and d[-1], which is when these are fast.
They are not first. Since `next(iter(d))` is O(n), following code is O(n^2) ``` while d: del d[next(iter(d))] ``` Making list is much faster than it. Following code is O(n). ``` for k in list(d): del d[k] ```
I found some related discussion in https://mail.python.org/archives/list/python-ideas@python.org/thread/QVTGZD6... but not this exact idea.
What is difference between your idea and previsous discussion? -- Inada Naoki <songofacandy@gmail.com>
On Mon, Oct 4, 2021 at 5:46 PM Erik Demaine <edemaine@mit.edu> wrote:
Have folks thought about allowing indexing dictionary views as in the following code, where d is a dict object?
d.keys()[0] d.keys()[-1] d.values()[0] d.values()[-1] d.items()[0] d.items()[-1] # item that would be returned by d.popitem()
since dicts were made order-preserving, indexing the keys, items, etc does make some sense.
I've also often wanted to get an arbitrary item/key from a dictionary, and
This is indeed one of the use cases identified. I found some related discussion in
https://mail.python.org/archives/list/python-ideas@python.org/thread/QVTGZD6... but not this exact idea.
That's a pretty different idea but this exact idea has been discussed on this list relatively recently. I still like it, but there wan't much general support. I'll leave it exercise for the read to find that thead, but it is there, and I suggest you look for it if you want to further pursue this idea. -CHB -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
participants (3)
-
Christopher Barker
-
Erik Demaine
-
Inada Naoki