I got this list of list:
a=[[0, 1, 1, 0, 'a'], [1, 0, 2, 0, 'c'], [2, 0, 0, 15, 2, 'g'], [1, 2, 0, 0, 'w'], [12, 0, 2, 3, 0, 2, 'front'], [0, 0, 0, 5, 2, 0, 'Z']]
indexA=[2, 4, 5]
Output: [[0, 1,2, 1,12,0,0 'a'], [1, 0,0, 2, 0,0 ,0'c'], [2, 0, 0, 15, 2,0,0'g'], [1, 2,15, 0,2, 0,0 'w'], [12, 0, 0,2, 0, 2,0 'front'], [0, 0, 0,0, 5, 0,0 'Z']]
Check my comments and let me know if you need more explanation :
a=[[0, 1, 1, 0, 'a'], [1, 0, 2, 0, 'c'], [2, 0, 0, 15, 2, 'g'], [1, 2, 0, 0, 'w'], [12, 0, 2, 3, 0, 2, 'front'], [0, 0, 0, 5, 2, 0, 'Z']]
index=[2, 4, 5]
for i in index:
if(i<len(a)): # to check if index is within bounds
for position,element in enumerate(a[i]):
if isinstance(element,int) and i!=position and position<len(a): # i!=position to skip inserting in the same sublist
a[position].insert(i,element)#insert(i,element) because the index the number is inserted follows the item in list index
Output:
[[0, 1, 2, 1, 12, 0, 0, 'a'], [1, 0, 0, 2, 0, 0, 0, 'c'], [2, 0, 0, 15, 2, 0, 2, 'g'], [1, 2, 15, 0, 2, 5, 0, 'w'], [12, 0, 2, 2, 3, 0, 0, 2, 'front'], [0, 0, 0, 5, 0, 2, 0, 'Z']]