+ 3

why the results aren't the same ?

lst =[1,-2,3,-4] for num in lst: if num>0 : num = 0 print( lst ) ......... lst = [ 1, -2, 3, -4] for i in range(len(lst)): if lst [i] >0: lst[i] = 0 print(lst)

20th Sep 2018, 10:15 AM
sama baluom
sama baluom - avatar
2 Respuestas
+ 5
The first code does not change anything from the list. num is a variable on its own, which might be changed in the if-branch, but it's not connected to the list, so no effect
20th Sep 2018, 10:35 AM
Matthias
Matthias - avatar
0
thanks;)
20th Sep 2018, 10:45 AM
sama baluom
sama baluom - avatar