List can't be change by normal for loop.why?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

List can't be change by normal for loop.why??

i=input().split() for n in i: n=n.title() print(i)# no change in string list for n in range(len(i)): i[n]=i[n].title() print(i)# change Can anyone explain me why???

14th Jun 2020, 9:03 AM
DEEPAK KUMAR
DEEPAK KUMAR - avatar
1 Answer
+ 2
for n in i just checks if n is in i, then n=n.title assign new value to n ,it makes no changes to original list whatsoever While i[n]=i[n].title() Changes the value at n index of list i
14th Jun 2020, 10:20 AM
Abhay
Abhay - avatar