Python Lists : Why new list object gets created after concatenation operation?

Viewed 100

I was going through the topic about list in Learning Python 5E book. I notice that if we do concatenation on list, it creates new object. Extend method do not create new object i.e. In place change. What actually happens in case of Concatenation?

For example

l = [1,2,3,4]
m = l
l = l + [5,6]
print l,m

#output
([1,2,3,4,5,6], [1,2,3,4])

And if I use Augmented assignment as follows,

l = [1,2,3,4]
m = l
l += [5,6]
print l,m

#output
([1,2,3,4,5,6], [1,2,3,4,5,6])

What is happening in background in case of both operations?

2 Answers
Related