The extend method
The extend method adds elements from the specified object, which can be another list, tuple, or set, to the list. In the method parameter, we specify the object we need to add.
Syntax
list.extend(object to add)
Example
Let's add another list to our list:
lst1 = ['a', 'c']
lst2 = ['1', '2']
lst1.extend(lst2)
print(lst1)
Result of code execution:
['a', 'b', '1', '2']
Example
Now let's add a tuple to our list:
lst = ['a', 'b']
tpl = ('1', '2')
lst.extend(tpl)
print(lst)
Result of code execution:
['a', 'b', '1', '2']
Example
Let's add a lot to our list:
lst = ['a', 'b']
st = {'e', 'f'}
lst.extend(st)
print(lst)
Result of code execution:
['a', 'b', 'e', 'f']