Python list extend
The extend() method is used to add elements of an iterable like a list, tuple or string to the end of the given list.
extend list
To extend a given list, use the extend() method as shown below.
# extend list using extend() method
numbers = [1, 2, 3]
numbers.extend([4, 5, 6])
print(numbers)
The list numbers contains the integer values 1, 2 and 3.
Then the extend() method is called and a list containing the integer values 4, 5 and 6 is passed.
Finally, the numbers list is printed and it contains a list of all integer values from 1 to 6 as shown below.
[1, 2, 3, 4, 5, 6]
syntax of extend()
The syntax of the extend() method is:
list.extend(iterable)
arguments of extend()
The extend() method accepts one argument. It can be an interable such as a list, tuple or string.
return value of extend()
The extend() method doesn't return any value, it modifies the original list by adding the elements of the given iterable.