Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 684
Yudi Avatar asked Mar 05 '16 03:03

Yudi


1 Answers

There are two methods being used there: __add__ and __iadd__. l + [5, 6] is a shortcut for l.__add__([5, 6]). l's __add__ method returns the result of addition to something else. Therefore, l = l + [5, 6] is reassigning l to the addition of l and [5, 6]. It doesn't affect m because you aren't changing the object, you are redefining the name. l += [5, 6] is a shortcut for l.__iadd__([5, 6]). In this case, __iadd__ changes the list. Since m refers to the same object, m is also affected.

Edit: If __iadd__ is not implemented, for example with immutable types like tuple and str, then Python uses __add__ instead. For example, x += y would be converted to x = x + y. Also, in x + y if x does not implement __add__, then y.__radd__(x), if available, will be used instead. Therefore x += y could actually be x = y.__radd__(x) in the background.

like image 191
zondo Avatar answered Oct 12 '22 13:10

zondo