Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading