Thomas is correct that this is a bit of an odd request unless explained
better.

There are a number of implicit assumptions that need to be revisited here.
Python Lists are what they are. They are not in any way tagged. They are not
linked lists or binary trees or dictionaries or whatever you are looking
for.

They are a mutable object with an order at any given time and no memory or
history of an earlier status.

They support not just insertion but also deletion and replacement and other
things.

But generally, if your time span between deciding on additions and
implementing them will contain no deletions, then one simple solution is to
re-order your insertion to always do the last one first. The indices will
only change at and above an insertion point. Your remaining insertions will
always be at an untouched region where the indices remain the same, for now.

A second choice as Thomas points out is to adjust your indices. An example
might be if you have a collection of proposed insertions and each contains
an index number and payload. Each time you insert the next payload at the
insertion point, you invoke a function that goes through your remaining
Collection and finds any with an index that is higher and increments it.

Obviously there are issues if dealing with adding multiple times to the same
index or adding multiple items at once.

The above could be encapsulated in some kind of VIEW in some languages
including of course some that use pointers.

I will add by pointing out a way to do a multi-insertion at once if you know
all the insertions at the same time.

Take your list that you want to change by adding at say positions 9, 3 and
6.

Now DON"T insert anything. Forget the concept.

Instead, and this is drastic, make a NEW list.

The new list is loosely old[0:2] + new_at_3 + old[3:5] + new_at_6 + old[6:8]
+new_at_9 + old[9:]

Something carefully written like that using concatenation means you do not
lose track of indices and end up with a new extended list you can feel free
to save under the old name and let the prior one be garbage collected.

Maybe one of the above hints at what could work for you, or others may
supply a better answer, or maybe you reevaluate what you are doing or
explain it some more.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail....@python.org> On
Behalf Of Thomas Passin
Sent: Friday, March 3, 2023 1:04 PM
To: python-list@python.org
Subject: Re: Python list insert iterators

On 3/3/2023 3:22 AM, 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)
> 
> No, it's not an option to sort the indexes and start inserting from the
> back.
> 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 ]
> 
> Does python have such functionality ?
> if yes, where can i find it ?

You should be more clear about how to establish the desired insertion 
point.  In your example, you first say that the insertion of B should be 
between 6 and 7. But after A gets inserted, you say that B's insertion 
point should change.  How is anyone able to know what the correct 
insertion point should be at any time?

If the rule is that B should get inserted after a particular known 
element, you can find out the index of that element with list.index() 
and insert just after that. If the rule is "There is an imaginary 
location that starts out after index 6 but moves depending on previous 
insertions", then you will probably need to capture a record of those 
insertions and use it to adjust the invisible insertion point.  But this 
synchronization could be tricky to keep correct depending on what you 
want to do to this list.

So you need to specify clearly what the rules are going to be.
-- 
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to