22
votes

Let's say I have these assignments:

points = []
point = (1, 2)

How come when I do this:

points += point

It works completely fine, and gives me points = [1, 2]. However, If I do something like:

points = points + point

It gives me a TypeError: can only concatenate list (not "tuple") to list. Aren't these statements the same thing, though?

1

1 Answers

22
votes

The difference, is that list += is equivalent to list.extend(), which takes any iterable and extends the list, it works as a tuple is an iterable. (And extends the list in-place).

On the other hand, the second assigns a new list to points, and attempts to concatenate a list to a tuple, which isn't done as it's unclear what the expected results is (list or tuple?).