Is it possible to update/modify the list elements/items in a loop. Here I have to modify items of t
n_wk=[1,2,3,2,3,4,2,3]
t=['a','a','a','a','a','a','a','a']
for i in range(len(n_wk)):
if i==0:
continue
if n_wk[i]<n_wk[i-1]:
if t[i]=='a':
t[i]='b'
elif t[i]=='b':
t[i]='c'
if n_wk[i]>n_wk[i-1]:
t[i]=t[i-1]
I was expecting output t = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c']. But, the output is coming out to be t=['a', 'a', 'a', 'b', 'b', 'b', 'b', 'b']. Seems like list t is not getting updated in the loop.
Can we not update item/elements of the list in a loop?
tis updated in the loop, since you replacedavalues withbvalues. Your expectation of when theelifmatches is incorrect.t[i]in theif n_wk[i]<n_wk[i-1]:ati=5be'b'. But, I checked it. It's coming out as'a'.