Python list insert iterators
Alan Bawden
alan at csail.mit.edu
Fri Mar 3 18:26:32 EST 2023
Guenther Sohler <guenther.sohler at gmail.com> writes:
Hi Python community,
I have a got an example list like
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
T T
and i eventually want to insert items in the given locations
(A shall go between 2 and 3, B shall go between 6 and 7)
Right now i just use index numbers to define the place:
A shall insert in position 2
B shall insert in position 6
However when i insert A in position 2, the index for successful insertion
of B gets wrong
(should now be 7 instead of 6)
No, it's not an option to sort the indexes and start inserting from the
back.
If you are definitely inserting from the front, then you can use
negative indexes, since the positions relative to the end of the list
won't be changing.
If you must insert in a random order, that won't help, but you haven't
told us what your real constraints are.
More information about the Python-list
mailing list