In this article, you’ll see how to extend a list in Python. The extend method is used to add the elements of a new list. It adds elements of the new list at the end of the original list. This method takes only one argument. The argument should be a list or a tuple. I highly recommend you get the “Python Crash Course Book” to learn Python.
Example 1: Extend a list
a = [5, 8, 10, 15, 20] a.extend([25, 30, 35]) print(a)
Output:
[5, 8, 10, 15, 20, 25, 30, 35]
Example 2: Add a tuple
a = [5, 8, 10, 15, 20] a.extend((3,6,9,15)) print(a)
Output:
[5, 8, 10, 15, 20, 3, 6, 9, 15]
Example 3:
a = [5, 8, 10, 15, 20] b = [25, 30, 35] a.extend(b) print(a)
Output:
[5, 8, 10, 15, 20, 25, 30, 35]