The extend() method adds all the elements of an iterable (list, tuple, string etc.) to the end of the list.
Example
Syntax of List extend()
The syntax of the extend() method is:
list1.extend(iterable)
Here, all the elements of iterable are added to the end of list1.
extend() Parameters
As mentioned, the extend() method takes an iterable such as list, tuple, string etc.
Return Value from extend()
The extend() method modifies the original list. It doesn't return any value.
Example 1: Using extend() Method
Output
Languages List: ['French', 'English', 'Spanish', 'Portuguese']
Example 2: Add Elements of Tuple and Set to List
Output
New Languages List: ['French', 'Spanish', 'Portuguese'] Newer Languages List: ['French', 'Spanish', 'Portuguese', 'Japanese', 'Chinese']
Other Ways to Extend a List
You can also append all elements of an iterable to the list using:
1. the + operator
Output
a = [1, 2, 3, 4]
2. the list slicing syntax
Output
a = [1, 2, 3, 4]
Python extend() Vs append()
If you need to add an element to the end of a list, you can use the append() method.
Output
[1, 2, 3, 4] [1, 2, (3, 4)]
To learn more, visit list append() method.