Raymond Hettinger added the comment:
Another take: Do a regular insertion with no special cases, followed by a pop
from the right:
>>> for i in range(len(d) + 1):
s = list(d)
s.insert(i, None)
_ = s.pop()
print(i, s)
0 [None, 'a', 'b']
1 ['a', None, 'b']
2 ['a', 'b', None]
3 ['a', 'b', 'c']
Nice parts:
* Doesn't change pop direction depending on the inputs
* Guarantee that entries to the left of i will keep their position.
* Post condition of d[i]==newelem applies whenever i exists.
Not nice part:
* Initially surprising that d.insert(len(d), newelem) does not actually insert
newelem.
* d.insert(len(d), newelem) not the same as d.append(newelem).
Another alternative is to raise an exception for the one case where
index==maxlen, with the theory being that d[index] won't be a valid index so
you can't insert anything there.
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue26194>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com