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 ?
>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.