A strange list concatenation result

Mok-Kong Shen mok-kong.shen at t-online.de
Sat Aug 13 17:03:46 EDT 2016


Am 13.08.2016 um 03:08 schrieb Steven D'Aprano:
> On Sat, 13 Aug 2016 06:44 am, Mok-Kong Shen wrote:
>
>>>>>> 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.
>
> Because you assign list2 = [1, 2, 3]. What did you expect it to be?

But in my function test() there is a code line "list2=list2+[4,5,6]".
Could you kindly explain why this didn't work?

M. K. Shen




More information about the Python-list mailing list