A strange list concatenation result

Mok-Kong Shen mok-kong.shen at t-online.de
Fri Aug 12 16:44:40 EDT 2016


Am 11.08.2016 um 23:49 schrieb Gary Herron:
> On 08/11/2016 03:06 PM, Mok-Kong Shen wrote:
>>
>> def test(list1,list2):
>>   list1+=[4,5,6]
>>   list2=list2+[4,5,6]
>>   print("inside ",list1,list2)
>>   return
[snip]

>> # With
>>
>> list1=[1,2,3]
>> list2=[1,2,3]
>> test(list1,list2)
>> print("outside",list1,list2)
>>
>> # I got the following:
>> # inside  [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]
>> # outside [1, 2, 3, 4, 5, 6] [1, 2, 3]
[snip]

> In this next example, there are two separate lists:
>
>>>> list1 = [1,2,3]
>>>> list2 = [1,2,3]
>>>> list1 += [4,5,6]
>>>> print(list1, list2)
> [1, 2, 3, 4, 5, 6] [1, 2, 3]
>
>
> Does that help?

I don't yet understand why in my 2nd example list2 came out as
[1, 2, 3] outside.

M. K.




More information about the Python-list mailing list