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

I tried converting the tuples in a list of tuples using range and code worked but when i tried the same using 'for i in list:' it is not working:

lst = [(8, 7), (9, 6), (4, 8), (3, 5), (9, 4)]

for i in range(len(lst)):
    lst[i] = list(lst[i])

this is working fine.

for i in lst:
    i = list(i)

but this is not working. I don’t know why ? Can anyone please tell me the reason ?

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

>Solution :

In your first code

for i in range(len(lst)):
    lst[i] = list(lst[i])

lst[i] is a variable which directly point to the element in list. Hence, any change in it effects the list itself.

Whereas in your second code

for i in lst:
    i = list(i)

i is a variable used in for loop and is not pointing to the list lst. Hence, any change to id doesn’t affect the lst.

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