The simplest and likely best way is to read the documentation at python.org or its wiki and other reliable sources. Once you learn how to read the specs it becomes relatively simple to get up to speed with any new concept, construct, function, etc. Keep the docs close at hand and refer to them often. That’s what we all do, still, even after years.
The built-in list.append()
method takes an object (value) and adds it to the right end of the list. It doesn’t need a self
parameter since self
in this case is the list we are appending. All it needs is a value. There is only one positional argument for this method.
>>> a = []
>>> a.append(1,2,3,4)
Traceback (most recent call last):
File "<pyshell#145>", line 1, in <module>
a.append(1,2,3,4)
TypeError: append() takes exactly one argument (4 given)
>>>
We would need to use a different built-in, or run this method four times to append all those values. The other method is list.extend()
which may come up in your reading of list
methods. Note below their subtle difference…
>>> a = []
>>> a.append(1,2,3,4)
Traceback (most recent call last):
File "<pyshell#152>", line 1, in <module>
a.append(1,2,3,4)
TypeError: append() takes exactly one argument (4 given)
>>> a.append([1,2,3,4])
>>> a
[[1, 2, 3, 4]]
>>> a = []
>>> a.extend(1,2,3,4)
Traceback (most recent call last):
File "<pyshell#156>", line 1, in <module>
a.extend(1,2,3,4)
TypeError: extend() takes exactly one argument (4 given)
>>> a.extend([1,2,3,4])
>>> a
[1, 2, 3, 4]
>>>
Again, only one positional argument but note how the append method treats the sequence compared to the extend method.
Above we mentioned that self
is the list object in context. It always refers to the instance object that is invoking the method. In classes we will always have self as an argument if the function is accessing any variables, instance or class. Likewise when one method calls another, the other method is also, self.method()
.