Given some Python list,
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
for some of its characters contained in another list, e.g.
list2 = ['a', 'd', 'e']
I need to add a character {char}1 to the index right before the original character in list1, such that the updated list1 looks like this:
list1 = ['a1', 'a', 'b', 'c', 'd1', 'd', 'e1', 'e', 'f', 'g']
My initial idea was to store the indices of original elements in a dictionary, and then insert new ones to [i-1] in list1,
in range (1, len(list1)+1). However, I then realised that this would only work for the first element, because the list would then grow and the indices would shift, so I would get wrong results.
Is there an efficient way to do it using insert?
list1? What about if{char}1is already inlist1?