Append versus ConcatenateΒΆ
The append
method adds a new item to the end of a list. It is also possible to add a new item to the end of a list by using the concatenation operator. However, you need to be careful.
Consider the following example. The original list has 3 integers. We want to add the word “cat” to the end of the list.
Here we have used append
which simply modifies the list. In order to use concatenation, we need to write an assignment statement that uses the accumulator pattern:
origlist = origlist + ["cat"]
Note that the word “cat” needs to be placed in a list since the concatenation operator needs two lists to do its work.
It is also important to realize that with append, the original list is simply modified.
On the other hand, with concatenation, an entirely new list is created. This can be seen in the following codelens example where
newlist
refers to a list which is a copy of the original list, origlist
, with the new item “cat” added to the end. origlist
still contains the three values it did before the concatenation. This is why the assignment operation is necessary as part of the
accumulator pattern.
Check you understanding
list-16-1: What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
alist = alist + 999
print(alist)