What is happening to my list?

Hi everyone, I was wondering what is happening to my list ‘lst’ in this case.

lst = [1, 2, 3, 4, 5] for i in lst: lst = i -1 print (lst)

It’s a list from 1-5 however, after I look through it and subtract 1 from each value, is it no longer a list? If i were to try print(lst[1]) after the for loop, I would receive the error int object is not subscriptable. My question is, what exactly is happening to lst ?

In your loop, look at what you are assigning to. To assign to an element in a list we need to use an index:

lst = [1, 2, 3]
lst[2] = 4
hint

In your code above you are assigning directly to a variable (lst) as opposed to an index within it.

Yeah that does make sense. So lst is now the last value assigned to it, in this case it’s 4. But how do i iterate through it while still maintaining the original list?

There are a lot of ways you can do this. The “traditional” method would probably be just to use a for loop, though we need to get each index of the list as opposed to each value.

For this we can use range() and len() :

lst = [1, 2, 3]

for i in range(len(lst)):
  print(i)

running the above code we’ll see each index of the list displayed. Now we can use this to modify each value:

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