-1

Can you explain to me why this code does not change the value of each element in the list?

li = [3,4,5]

for elm in li:
    elm = 1
print(li)
Navy Alsk
  • 27
  • 8

1 Answers1

0

No, it does not. it changes the value of the elm variable. To change the value of the list element you have to use following code.

li = [3,4,5]

for i in range(len(li)):
    li[i] = 1
print(li)
Aleksander Ikleiw
  • 2,549
  • 1
  • 8
  • 26