Do I have to use "append"?

If I have to add Items to a list with “append”, I can only add one; but I can add more if I use “+=”.
Why would I use “append” then? It’s larger and they make the same thing.
I have the feeling that I may be missing something. Please help.

Because it is still useful for pushing items onto the end of the list.

+= is the equivalent of list.extend().

Let’s explore use cases…

>>> a = []
>>> a.append(3,4,5)
Traceback (most recent call last):
  File "<pyshell#37>", line 1, in <module>
    a.append(3,4,5)
TypeError: append() takes exactly one argument (3 given)
>>> a.extend(3,4,5)
Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    a.extend(3,4,5)
TypeError: extend() takes exactly one argument (3 given)
>>> a += 3, 4, 5
>>> a
[3, 4, 5]
>>> a += 6,
>>> a
[3, 4, 5, 6]
>>> a.extend(7)
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    a.extend(7)
TypeError: 'int' object is not iterable
>>> 
2 Likes