Python list insert iterators

dn PythonList at DancesWithMice.info
Fri Mar 3 16:47:40 EST 2023


On 03/03/2023 21.22, Guenther Sohler wrote:
> 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)

The danger here is using the index to have meaning beyond its function 
(a position relative to the beginning/end).


> No, it's not an option to sort the indexes and start inserting from the
> back.

Without explaining this criteria (and any others), suggestions can only 
be guesses!

> The most elegant option is not to store indexes, but list iterators, which
> attach to the list element
> and would automatically move, especially if an element is inserted before.
> 
> I could not find such functionality in python lists of [ 1,2,3 ]

So, if list doesn't float-your-boat, what about moving to something else?

> Does python have such functionality ?
> if yes, where can i find it ?

dict - where the keys indicate relative position

linked-list (https://en.wikipedia.org/wiki/Linked_list) where each value 
is accompanied by a "pointer" which addresses the next value by-index; 
and insertion/deletion is possible by exchanging pointers.
-- 
Regards,
=dn


More information about the Python-list mailing list