![]() |
VOOZH | about |
extend() method is used to add all elements from another iterable to the end of a list. It updates the original list by adding each element one by one.
In this example, extend() adds all elements of one list to another list.
[1, 2, 3, 4, 5]
Explanation: extend(b) adds all elements of list b to the end of list a.
list.extend(iterable)
Example 1: In this example, elements of a tuple are added to a list using extend(). Each tuple element is inserted separately into the list.
[1, 2, 3, 4]
Explanation: extend(b) adds all elements of tuple b to list a.
Example 2: Here, a set is used with extend(). All elements of the set are added to the list.
[10, 20, 40, 30]
Explanation: extend(b) inserts all elements from set b into list a. The order may vary because sets are unordered.
Example 3: This example, passes a string to extend(). Each character of the string is added separately to the list.
['P', 'y', 't', 'h', 'o', 'n']
Explanation: extend(b) adds each character of the string b separately to the list.