Message258947
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. |
|
Date |
User |
Action |
Args |
2016-01-26 06:38:23 | rhettinger | set | recipients:
+ rhettinger, tim.peters, mark.dickinson, serhiy.storchaka |
2016-01-26 06:38:23 | rhettinger | set | messageid: <1453790303.44.0.872538997694.issue26194@psf.upfronthosting.co.za> |
2016-01-26 06:38:23 | rhettinger | link | issue26194 messages |
2016-01-26 06:38:23 | rhettinger | create | |
|