Python's list methods append and extend add items to the list but the difference is that
we use append when a single item is to be added in the list
while we use extend when more than one item is to be added.
For Example:
Items = [23,34,56] #create a list
Items
Output
[23, 34, 56]
Items.append([10,40]) #append one element
Items
Output
[23, 34, 56, [10, 40]]
Python will give an error if we try to append more than one item
Items.append(1,2,3)
Items
Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-e5c36eb74f1f> in <module>
----> 1 Items.append(1,2,3)
2 Items
TypeError: append() takes exactly one argument (3 given)
While in case of Extend more than one item can be added, extend will extend the list without nesting the list, unlike append.
For Example:
Items = [23,34,56]
Items
Output
[23, 34, 56]
Items.extend([10,20,30])
Items
Output
[23, 34, 56, 10, 20, 30]
Hope this helps!