In many cases, this may be an XY Problem. Why are you indexing your dictionary keys by position? Do you really need to? Until recently, dictionaries were not even ordered in Python, so accessing the first element was arbitrary.
I just translated some Python 2 code to Python 3:
keys = d.keys()
for (i, res) in enumerate(some_list):
k = keys[i]
# ...
which is not pretty, but not very bad either. At first, I was about to replace it by the monstrous
k = next(itertools.islice(iter(keys), i, None))
before I realised this is all much better written as
for (k, res) in zip(d.keys(), some_list):
which works just fine.
I believe that in many other cases, indexing dictionary keys by position can be avoided. Although dictionaries are ordered in Python 3.7, relying on that is not pretty. The code above only works because the contents of some_list
had been recently produced from the contents of d
.
Have a hard look at your code if you really need to access a disk_keys
element by index. Perhaps you don't need to.