This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: List inplace addition different from normal addition
Type: behavior Stage: resolved
Components: Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: devkapilbansal, eric.smith, rhettinger, xtreak
Priority: normal Keywords:

Created on 2021-09-26 12:13 by devkapilbansal, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg402658 - (view) Author: Kapil Bansal (devkapilbansal) Date: 2021-09-26 12:13
Hi,

I tried addition and in-place addition on a list.

>>> l = ['a', 'b', 'c']

>>> l = l + 'de' (raises an error)

>>> l += 'de'
>>> print(l)
['a', 'b' , 'c', 'd', 'e']

I want to ask why the behaviour of both these are different?? If it is done intentionally, then it should be there in the documentation but I am not able to find any reference.
msg402659 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-09-26 12:34
For those not in front of a computer, the error is:

>>> l = l + 'de'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
msg402660 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-09-26 12:54
https://docs.python.org/3/faq/programming.html#faq-augmented-assignment-tuple-error

> for lists, __iadd__ is equivalent to calling extend on the list and returning the list. That’s why we say that for lists, += is a “shorthand” for list.extend

This example is calling extend on list of strings with another string as argument. Hence the target string is iterated and each character is added as an element. __add__ is different compared __iadd__. For += __iadd__ is called if defined and falls back to __add__
msg402679 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-09-26 20:58
Kapil, this behavior was intentional (not a bug), but later regarded as a design mistake.  GvR has said that if he had to do it over again list.__iadd__ would only accept other lists.  The problem arises when people write "somelist += 'hello'" and expect it to add a single string with one word rather than five one letter strings.  That said, the current behavior is guaranteed, people rely on it, and we cannot change it.

As Karthikeyan points out, this is documented.  However since the docs have grown so voluminous, it is often difficult to find any one particular fact.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89456
2021-09-26 20:58:22rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg402679

resolution: not a bug
stage: resolved
2021-09-26 12:54:50xtreaksetnosy: + xtreak
messages: + msg402660
2021-09-26 12:34:08eric.smithsetnosy: + eric.smith
messages: + msg402659
2021-09-26 12:13:59devkapilbansalcreate