On Mon, Jun 29, 2020 at 9:12 PM Hans Ginzel email@example.com wrote:
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()?
Because dict is optimized for random access by key and iteration, but not for random access by index.
items = [*d.items()] for i in range(len(items)): do(items[i])
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.