When manipulating lists, you have access to two methods called append() and extend(). The former appends an object to the end of the list (e.g., another list) while the latter appends each element of the iterable object (e.g., another list) to the end of the list.
For example, we can append an object (here the character ‘c’) to the end of a simple list as follows
>>> stack = ['a','b']
>>> stack.append('c')
>>> stack
['a', 'b', 'c']
If we append a list made of several items, the result as expected is (a list is an object):
>>> stack.append(['e','f'])
>>> stack
['a', 'b', 'c', ['e', 'f']]
However, sometimes what we want is to append all the elements contained in the list rather the list itself. You can do that manually of course, but a better solution is to use extend() as follows:
>>> stack.extend(['g','h'])
>>> stack
['a', 'b', 'c', ['e', 'f'], 'g', 'h']
18 Responses to Python list: difference between append and extend