Change the values in the nested list according to the specified index list

How to change the values in nested list

How to change the values in the list base on the indexes To clarify, I originally have the_list

the_list = [['a','a','a'],['b','b','b'],['b','b','b'],['c','c','c']]

However, I would like to change the values in the_list above that have the index/position in the indexA

indexA = [(0,2),(1,2),(0,1)]   

which I would like to change the following index of the_list to ‘hi’

the_list[0][2]      
the_list[1][2] 
the_list[0][1]  

Therefore, the expected output is

the_list = [['a', 'hi', 'hi'], ['b', 'b', 'hi'], ['b', 'b', 'b'],['c', 'c', 'c']]  

Currently I’m doing it manually by what follows:

the_list = [['a','a','a'],['b','b','b'],['b','b','b'],['c','c','c']]     
the_list[0][2] = 'hi'     
the_list[1][2] = 'hi'      
the_list[0][1] = 'hi'     

Output:

the_list = [['a', 'hi', 'hi'], ['b', 'b', 'hi'], ['b', 'b', 'b'],['c', 'c', 'c']]

Please kindly recommend the better way of doing it

Thank you in advance

>Solution :

for i, j in indexA:
    the_list[i][j] = 'hi'

print(the_list)

gives

[['a', 'hi', 'hi'], ['b', 'b', 'hi'], ['b', 'b', 'b'], ['c', 'c', 'c']]

Leave a Reply