On Mon, Jun 29, 2020 at 9:12 PM Hans Ginzel <h...@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 <songofaca...@gmail.com> _______________________________________________ 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/KCCVM6PRXZPMTUQEKOF4VAG6ATS4XFSK/ Code of Conduct: http://python.org/psf/codeofconduct/