
On Mon, Jun 29, 2020 at 9:12 PM Hans Ginzel <hans@matfyz.cz> wrote:
Tahnk you,
On Fri, Jun 26, 2020 at 10:45:07AM -0700, Brett Cannon wrote:
Why can't you do `tuple(dict.items())` to get your indexable pairs?
of course, I can. But how it is expensive/effective? What are the reasons, why object dict.items() is not subscriptable – dict.items()[0]?
Because dict is optimized for random access by key and iteration, but not for random access by index. For example: sample 1: items = [*d.items()] for i in range(len(items)): do(items[i]) sample 2: for i in range(len(d)): do(d.items()[i]) # if dict_items supports index access. sample 1 is O(n) where n = len(d), but sample 2 is O(n^2). By not supporting index access, dict_items prevents to write such inefficient code. -- Inada Naoki <songofacandy@gmail.com>