To add some numbers to this discussion.
Using the patch I provided earlier, I tried running some performance comparisons against different call patterns & dictionary sizes:
These tests have two versions:
direct_index: uses the __getitem__ method in the patch (O(n)) on the view
list_copy: converts the view into list(), then does an index lookup
Each score is the fastest result of running the test 10 times.
Accessing the last item [-1] using the __getitem__ approach is the *worst-case scenario* for the __getitem__ method.
d.keys()[-1] vs list(d.keys())[-1]
Items in dict: 1 direct_index: took 1us list_copy: took 2us Speedup: 1.85x
Items in dict: 26 direct_index: took 1us list_copy: took 2us Speedup: 2.18x
Items in dict: 650 direct_index: took 1us list_copy: took 6us Speedup: 7.68x
Items in dict: 15,600 direct_index: took 6us list_copy: took 119us Speedup: 19.38x
Items in dict: 358,800 direct_index: took 129us list_copy: took 6065us Speedup: 47.12x
Items in dict: 7,893,600 direct_index: took 2827us list_copy: took 210419us Speedup: 74.43x
random.choice(d.items()) vs random.choice(list(d.items()))
Items in dict: 1 direct_index: took 3us list_copy: took 3us Speedup: 1.23x
Items in dict: 26 direct_index: took 3us list_copy: took 5us Speedup: 1.75x
Items in dict: 650 direct_index: took 5us list_copy: took 19us Speedup: 4.23x
Items in dict: 15,600 direct_index: took 7us list_copy: took 2771us Speedup: 377.55x
Items in dict: 358,800 direct_index: took 112us list_copy: took 76626us Speedup: 682.33x
Items in dict: 7,893,600 direct_index: took 1667us list_copy: took 1602864us Speedup: 961.37x
d.items()[0] vs list(d.items())[0]
Items in dict: 1 direct_index: took 1us list_copy: took 1us Speedup: 1.75x
Items in dict: 26 direct_index: took 1us list_copy: took 2us Speedup: 1.84x
Items in dict: 650 direct_index: took 1us list_copy: took 19us Speedup: 30.87x
Items in dict: 15,600 direct_index: took 1us list_copy: took 2867us Speedup: 4777.73x
Items in dict: 358,800 direct_index: took 1us list_copy: took 73268us Speedup: 112719.85x
Items in dict: 7,893,600 direct_index: took 1us list_copy: took 1604230us Speedup: 2765900.12x
So, use-case discussions aside, you get some pretty convincing performance wins even on small-ish dictionaries if direct indexing is used.
Steve