0
Python - How come adding another list of integers to an original list won't give the same output as appending the same integers?
How come the following codes won't give the same output? 1) def zzz(list1): list2 = list1 list2.append(4) list2.append(5) list2.append(6) list1 = [1, 2, 3] zzz(list1) print(list1) # outputs [1, 2, 3, 4, 5, 6] 2) def zzz(list1): list2 = list1 list2 + [4,5,6] list1 = [1, 2, 3] zzz(list1) print(list1) # outputs [1, 2, 3] ========================================================= From my understanding, the list2 (local) is referencing the PARAMETER list1 (global). Does referencing only work when modifying the reference variable with a METHOD? Does local list2 stop acting as the reference when a + operator is applied? For what reason?
1 Resposta
+ 2
In 2nd one, you are not storing back addition..
list2+[4,5,6] this result you missing..
Change this to list2 += [4,5,6]
And see result...